1

Here is the example code:

<div> <div> </div> <div> </div> <div> </div> .... </div> 

I just need to find the index of the the inner div in the parent div, passed to function as this.

p.s. I checked the other question which is same: Is it possible to get element's numerical index in its parent node without looping?

however that method suggested did not work for me.. Is it possible to get element's numerical index in its parent node without looping?

2 Answers 2

1

Here's a working example using plain JS (no jQuery): http://jsfiddle.net/jfriend00/xgk4y/. Click on any of the divs to see the index returned from countPrevSiblings().

And the code from that:

function countPrevSiblings(elem) { var i = 0; while((elem = elem.previousSibling) != null) { // count element nodes only if (elem.nodeType == 1) { ++i; } } return i; } 
Sign up to request clarification or add additional context in comments.

Comments

0

You can get index by using index() jquery function, if this is not working for you, explain you question bit more.

Sample : $(this).index($(this).siblings());

1 Comment

jquery wasn't tagged on this question, which typically means OP wants a pure-js solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.