javascript - Set the background-image values of all divs of a specific class based on ids -
i'd write script automatically sets background-image url of divs on page classes .img-cover based on value of each ones 'data-image' attribute. way whenever want set background-image can add correct 'data-image' value , class div , automatically pull correct image.
update
css doesn't seem having periods in ids, removed '.jpg' ids , added script, , swapped out data attribute ids. here's html:
<div class="img-cover" data-image="flags_install"></div>
the new jquery:
$(".img-cover").each(function() { var data = $(this).attr('data-image'); console.log(data); $(this).css("background-image", "url('/img/cover-imgs/" + data + ".jpg');"); console.log($(this).css("background-image")); });
i added console logs , getting data values first 1 it's returning "none" background-image 1 still. other thoughts?
because of naming convention "id="flags_install.jpg" think .jpg parsed class applied element.
try approaching via id="flags_install" , $(this).css("background-image", "url('/img/cover-imgs/" + id + ".jpg');");
working:
$(this).css('background-image', 'url(/img/cover-imgs/' + data + '.jpg)');
Comments
Post a Comment