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

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -