html - Can't get jQuery .each() to work -
i have html code in 3 sections of page:
<div class="pjesmarrje"> <a href="#" onclick="window.open('https://www.facebook.com/sharer/sharer.php?u='+encodeuricomponent(location.href),'facebook-share-dialog','width=626,height=436'); return false;"> <div></div> <span>kliko këtu për pjesëmarrje</span> </a> </div>
and, trying change background image of div inside when clicked. got jquery code:
$(document).ready(function() { $(".pjesmarrje").click(function() { $(".pjesmarrje div").css("background-image", "url(images/mumanin_s2.png)"); }); });
when click 1 of elements, others background images changed too. don't want happen, want bg image changed when particular element clicked. tried use .each() function, didn't work.
any appreciated. thanks.
you're losing referred scope. if want work within _that specific .pjesmarrje
want like:
$(document).ready(function() { $(".pjesmarrje").click(function() { // `this` reference `.pjesmarrje` triggered click // event. and, within `<div>` want find `<div>` // background want change. $("div", this).css("background-image", "url(images/mumanin_s2.png)"); }); });
note second argument: $(selector, scope)
. means care <div>
within clicked .pjesmarrje
(and not on page).
and food thought: $('div', this)
synonymous $(this).find('div')
.
Comments
Post a Comment