I have several li
elements that get added to a ul
list dynamically. In each of the li
elements, there's a button. I want to attach a click event to each button, and within the event handler, I want to get properties unique to the li
whose button was clicked.
This (non-functional) code illustrates what I want:
$('ul > li > button').each('click', function(){
var asdf = $('somehow access any arbitrary element in the li whose button was clicked').html();
});
My current solution (below) works, but it forces me to set an id for each li
that indicates its position in the list, which for various reasons I'd rather not do.
// In the response function of the AJAX call that populates the list:
$('ul > li').each(function(i){
$('button', this).click(function(){
var name = $('ul > li#item'+i+' > .name').html();
});
});
Is there a better way?