panthro panthro - 3 months ago 26
jQuery Question

jQuery: get parent, parent id?

<ul id ="myList">
<li><a href="www.example.com">link</a></li>
</ul>


How can I get the id of the ul (myList) using jQuery? My j-script event is triggered when the a link is clicked. I have tried:

$(this).parent().attr('id');


But it gets the id of the li, I need to go one higher, and I cant attach the click to the li either.

Answer
$(this).parent().parent().attr('id');

Is how you would get the id of the parent's parent.

EDIT:

$(this).closest('ul').attr('id');

Is a more foolproof solution for your case.