I am trying to select the id of a <li>
element that is being created under another dynamic element, an <a>
tag. I cannot seem to get a handle on the id of <li>
. The closest I've got is this:
$("a li:first-child").attr('id');
but will only give the id of the first li, not the one that is being clicked.
Here is the script, which I have truncated, because the only the first part is important:
$.each(...
$("#inventoryDiv").append("<ul id='invList'></ul>");
$("#invList").append("<a href='javascript:void(0);' name='invLink'><li id='" + this.inventory_id + "'>
...
and my listener is:
$("#inventoryDiv").on('click','a',function(){
console.log($("a li:first-child").attr('id');
}
Any thoughts?