Possible Duplicate:
Getting a parent element for a jquery selector
I have a list of item :
<div id="wrap"><ul class="thumbs noscript">
<li id="1">
<a class="thumb" href="photos/10-01.jpg" title="" />
<img src="photos/10-01_thumb.jpg" alt="" />
</a>
<div class="caption">
<div class="download"><a href="#" class="add2cart">Save Image</a></div>
<div class="image-title">Titre de la photo #1</div>
<div class="image-desc">Description de la photo #1</div>
</div>
</li> /* and so on with li's and different ID's... */
I can’t get the value of the li ID, therefore, the title.
Have read and tried almost every answer on here, they won’t work once I test on my site…
Any idea ? TY folks
$(document).ready(function(){
$("#wrap > ul > li > .caption > .download > .add2cart").click(function(){
var thisID = $(this).closest('li').attr("id");
alert(thisID);
});
});
RIGHT ANSWER (see below) : insomiac, tyvm
And thanks also to Diodeus for pointing out ID problem.
Thanks to all the folks who answered 😉
Store the value using “data” api and then on click retrieve that value instead of using id.
jquery :
Works on jsfiddle
Update to your js code:
Note : As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. In addition, .attr() should not be used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the .prop() method.