<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.
The parent() method returns the direct parent element of the selected element. The DOM tree: This method only traverse a single level up the DOM tree. To traverse all the way up to the document's root element (to return grandparents or other ancestors), use the parents() or the parentsUntil() method.
parent() so $(this). parent() but this would get the parent div of the input.
$(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.
$(this).closest('ul').attr('id');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With