Search code examples
jquerytraversal

jQuery: Given just an element ID name, how do it find its position in the dom relative to its parent?


This one has me stumped.

<div id="container">
    <div id="store"></div>
    <div id="contact"></div>
</div>

I'm looking for the contact divs position as child of "container". So just to be extra clear, if i'm looking for 'contact's position, i need jquery to return the number 1.

any help is greatly appreciated! thanks!


Solution

  • You could try something like:

    $('#contact').prevAll('div').length + 1
    

    as in get the length of previous div siblings. Omit the +1 if you want a zero-based index.