jeudi 26 mars 2015

Index of an element in a filtered jQuery set


So I have been searching for a couple of hours now and I haven't really found a solution yet.


It seems that the jQuery method .index() when calling on an element in a set, does not take the selector into regard.


To clarify, when I have a list of 5 li elements, where element 2, 3 and 4 have the class "foo". When I first filter these items with $('li.foo'), I get a set back with the size of 3 elements big. When I perform the .index() on the first item in the set like so $('li.foo').first().index(), instead of returning 0 (since it's the first item in the set and index starts counting from 0), it actually returns 1 (or in other words, the index for the second item).


Now with the example above it doesn't really seem necessary in my example to use index, but in my code I actually filter the filtered set( $('li.foo').filter('.active') ) to get a single item and then get the index of that item.


The problem it seems is that .index ignores the filters and selectors and I haven't had any luck in $.inArray();


If anyone could shed some light on how to get the index with a sure fire way, I would be super grateful!!





Aucun commentaire:

Enregistrer un commentaire