Hi, Thanks for helping. I think I haven't explained properly what I wanted....
I'm looking for the first parent with an attribute 'id', which in this example was <div id="main">. However it could be anything, not necessary "main", so your code wouldn't work as you are looking specifically for "#main". I think your .each() function is correct, however I can't test right now, but will give it a go. Thanks for that. :) On 19 Dec, 21:33, Ricardo Tomasi <ricardob...@gmail.com> wrote: > 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.