Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Traverse up until some element

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];
like image 975
marks34 Avatar asked Mar 04 '11 19:03

marks34


2 Answers

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

elements.closest("div.vote");
like image 97
James Sulak Avatar answered Sep 21 '22 13:09

James Sulak


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"); 
like image 23
Chandu Avatar answered Sep 21 '22 13:09

Chandu