Thursday, 15 August 2013

javascript - Using image path as div background -



javascript - Using image path as div background -

i have code found on post, showing how replace image divs, while keeping attributes of image. want image source background-image of div replacing it..

the code found is

$('.produkt_billede > img').each(function(){ $div = $('<div>').html($(this).attr('src')); $(this).replacewith($div); });

my desired result is:

<div style="background-image: url('image.png');" class="something"></div>

thanks.

what need define appropriate attributes of <div> when create it:

$('.produkt_billede > img').each(function(){ var path_to__image = $(this).attr('src'); $div = $('<div>', { 'style': 'background-image: url("'+ path_to__image +'");', 'class': 'something' }); $(this).replacewith($div); });

when creating element, sec parameter can object defining new attributes it. in case, i'm defining style , class attributes; can add together more if require.

javascript jquery

No comments:

Post a Comment