0

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);

1
  • JQuery's index() function is described here: api.jquery.com/index Commented Jun 11, 2016 at 2:39

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.