ahren ahren - 3 years ago 165
Javascript Question

Get position of selected elements, not index of element in DOM

<div>
<h3>Heading one</h3>
<h3>Heading two</h3>
<h4>heading caption</h4>
<h3>Heading three</h3>
</div>‚Äč


I want to get the position of the element within the jQuery selection, rather than it's index in the DOM.

At the moment, if I
alert($("h3").eq(2).index());
I'll get '3' - I want '2'.

http://jsfiddle.net/4QmEF/

Answer Source

Pass your index() a selector:

alert($("h3").eq(2).index("h3"));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download