views:

366

answers:

3

is there a better way to select grandparents elements in jquery in order to avoid this ?

$(this).parent().parent().parent().parent().parent().children(".title, .meta").fadeIn("fast");

thanks

+3  A: 

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/

Neil Aitken
+1  A: 

Use the parents() selector to get all parents of an element. You can then either search the collection, or iterate over it if you want to affect all ancestors.

Traveling Tech Guy
+1  A: 

In addition to parents(), as they've said, you should also check out closest(). Read the comparison in the documentation there, but its main differences are that it searches for only one result, and it includes $(this) in what it searches (could get the thing you're searching from if you're not specific enough). Pros and cons.

D_N
That's nifty, I've never used that method before.
Neil Aitken