To get the #main parent you'd use

$('h2').parents('#main')

You can get indexes for all parents in 'inside out' order, or use the
index() function to calculate them:

$('h2').parents().each(function(index){
   // p index == 0
   // div index == 1
   // #main index == 2
   $(this).parent().children().index(this); //should give you the
indexes you want (p==2,div==0)
});

Hope you get the idea. Check the documentation (docs.jquery.com/
Selectors, docs.jquery.com/Traversing) in case of doubt :)

- ricardo


On Dec 19, 2:13 pm, "graphic...@googlemail.com"
<graphic...@googlemail.com> wrote:
> Hi,
>
> Consider the following html :
>
> <div id="main">
>   <div>
>       <p/>
>       <p/>
>       <p>
>          <h2> target node </h2>
>       </p>
>   </div>
> </div>
>
> Starting from the h2 node, I want to find its closest parent with an
> id, in this case the div with id="main".  I also want to store the
> index position of all of the parents of the target. In this case that
> would be 0 (for the div right under "main") and 2 (for the third <p>)
>
> How can I do that using jQuery ? I hope what I ask is clear enough.

Reply via email to