I’m doing some quite rudimentary jQuery stuff, getting started really, and I’m frequently navigating up the dom by doing things like
$(this).parent().parent().addClass('hello');
I was just wondering if there’s a nicer way to do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use
parents, which returns all ancestor elements in turn. If you want to stop traversing at a particular level, useeqto filter the resulting collection. For example, to get the grandparent:If you want to go upwards through the tree and stop not at a particular level, but at a particular selector match, use
closestinstead, e.g.: