Is there a better way to select grandparent elements in jQuery in order to avoid this ?
$(this).parent().parent().parent().parent().parent().children(".title, .meta").fadeIn("fast");
Thanks.
jQuery parentsUntil() Method The parentsUntil() method returns all ancestor elements between the selector and stop. An ancestor is a parent, grandparent, great-grandparent, and so on.
jQuery parent() Method 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.
parent() method returns the direct parent element of the selected one. This method only traverse a single level up the DOM tree. parents() method allows us to search through the ancestors of these elements in the DOM tree.
I can use $('#root > * > *') to get all its grandchildren.
You can use the parents()
method which matches parents against a selector
http://api.jquery.com/parents/
Or if you're using 1.4 there is a new parentsUntil()
method
http://api.jquery.com/parentsUntil/
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