Get position of selected elements, not element index 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 an element inside jQuery, not the index in the DOM.

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

http://jsfiddle.net/4QmEF/

+4
source share
2 answers

Pass your index () selector:

 alert($("h3").eq(2).index("h3")); 
+7
source
 <div> <h3>Heading one</h3> <h3>Heading two</h3> <h4>heading caption</h4> <h3>Heading three</h3> </div> <script type="text/javascript"> $(document).ready(function(){ $("div").children().each(function(){ $(this).click(function(){ alert($(this).context.innerHTML); }); }); }); </script>` 
0
source

All Articles