Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reach the uncle using jquery

<div id="grandfather">
  <div id="uncle"></div>
  <div id="father>
    <div id="me"></div>
  </div>
</div>

I am at $("#me") , and I want to select my uncle, using stuff like :

 $("#me").find("#uncle")
 $("#me").next("#uncle")
 $("#me").prev("#uncle")

How ?

like image 809
Ali Bassam Avatar asked May 05 '12 06:05

Ali Bassam


1 Answers

You could use $.parent and $.prev assuming your uncle is always above your father:

$(this).parent().prev(); // where 'this' is #me

You could also go all the way up to your grandfather, and find uncles from there:

$(this).parents("#grandfather").find(".uncles");

Or you could search your father's siblings:

$(this).parent().siblings("#uncle");

I would encourage you to read the Traversing portion of the jQuery API for various other methods.

like image 62
Sampson Avatar answered Sep 21 '22 11:09

Sampson