Tester232323
Tester232323

Reputation: 301

Get index of jquery object within another jquery object

Suppose

<div class='foo' id='a'></div>
<div class='foo' id='b'></div>
<div class='foo' id='c'></div>

var foos = $('.foo');

we can get the first foo if we want

var myFoo = $(foos.get(0));

Now that we have myFoo, how can we do the opposite to get its index?

var index = foos.getIndexOf(myFoo);

Upvotes: 0

Views: 22

Answers (1)

Rayon
Rayon

Reputation: 36609

Use .index( element )

If .index() is called on a collection of elements and a DOM element or jQuery object is passed in, .index() returns an integer indicating the position of the passed element relative to the original collection.

var foos = $('.foo');
var myFoo = $(foos.get(0));
console.log(foos.index(myFoo));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class='foo' id='a'></div>
<div class='foo' id='b'></div>
<div class='foo' id='c'></div>

Upvotes: 1

Related Questions