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
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
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
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.
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.