Traverse up until some element
Asked Answered
K

3

8

I have some element selected with jQuery selector. It is en element from list of elements with the same class ('my-elements'). Now I'd like to select element containing the selected one which is several levels above. How can I do this ?

<div class="vote>
(... several levels)
<div class="my_element"></div>
</div>

the selector:

var elements = $('.my_element');
var element = elements[0];
Kovno answered 4/3, 2011 at 19:20 Comment(0)
T
14

The .closest() method should work (http://api.jquery.com/closest/):

elements.closest("div.vote");
Taft answered 4/3, 2011 at 19:22 Comment(0)
V
1

If I understand it correctly you want to traverse to div.votes from div.my_element Try this:

var elements = $('.my_element'); 
var element = elements.closest("div.vote"); 
Vincenza answered 4/3, 2011 at 19:23 Comment(0)
S
0

If you're attempting to find the container element for your current object I suggest the parents() method. It takes a selector, or without one, returns all of the parents of the element.

Schlesien answered 4/3, 2011 at 19:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.