Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: selecting grandparents

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.

like image 994
aneuryzm Avatar asked Mar 03 '10 09:03

aneuryzm


People also ask

How do I target grandparents in jQuery?

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.

How do I find a specific parent in jQuery?

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.

What is the difference between parent () and parents () methods in jQuery?

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.

How can I get grandchild in jQuery?

I can use $('#root > * > *') to get all its grandchildren.


1 Answers

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/

like image 81
Neil Aitken Avatar answered Sep 19 '22 09:09

Neil Aitken