tags:

views:

27

answers:

3

Hi,

http://api.jquery.com/index/

Is there a way to use jQuery index() method to find the index of an element related to the overall document?

I mean, if I have a bunch of img tags in a document, is there a way to know, regardless of their position in the document, their index?

example :

<body>
<img />
<div>
   <img />
   <div>
      <img />
   </div>
</div>
</body>

I've tried using $(window), $(body), $(document), but they all fail. I really need the overall index of the element.

Cheers!

+1  A: 

Do you mean

$('img').index(imgElement);

or

$('*').index(imgElement);

?

Nikita Rybak
Thanks, it worked!
yoda
+1  A: 

index() returns the position in a given collection:

$('img').index('id');
Evan Trimboli
that's the thing, thanks!
yoda
A: 

$('*').index($('.someClass'))

lacroix1547
that can be tricky if the class exists in more than one element.
yoda