views:

133

answers:

3

Is $('*').index(currentElement) will give a unique number?
i am asking because i can't understand the index method good from the JQuery docs

+2  A: 

"Yes". (I should qualify this with, as long as the context of the index is always the entire DOM) Otherwise, as the other answer states, the answer is no.

It will give you the index of the element within all DOM elements. If the DOM changes, it will no longer be valid.

The reason for needing an index like this, would have to be very unusual, and I would strongly suspect there's a better way to do what you are trying to accomplish.

altCognito
+1  A: 

The index method will search at what index the current element is. I can explain it a lot better in code:

 <div id='div0'></div>
 <div id='div1'></div>
 <span id='span0'></span>
 <div id='div2'></div>  

 $('div').index( $('#div0')[0] ); //index is 0 as it is the first of all divs
 $('*').index( $('#div0')[0] ); //index is 0 as it is the first of all elements
 $('span').index( $('#span0')[0] ); //index is 0 as it is the first of all spans
 $('*').index( $('#span0')[0] ); //index is 2 as it is the third of all elements
Pim Jager
+2  A: 

Yes, it will return the index where you can find your element in your jQuery collection - e.g.

var allElements = $("*");
var index = allElements.index(someElement);

if(allEmenets[index] == someElement){
  alert("Found it!");
}
Seb