5
<ul>
  <li>Kelvin</li>
  <li>Jerry</li>
  <li>Adi</li>
  <li>Dani</li>
  <li>Olvin</li>
</ul>

How to get the index of the <li> element that contains "Jerry"?

I've tried to read these

But can't find the answer :( Can you help me with that?

2 Answers 2

6

Using jQuery's .index will give you the index of an element in the given elements:

var index = $('li').index($('li:contains("Jerry")'));
Sign up to request clarification or add additional context in comments.

2 Comments

SyntaxError: missing ) after argument list [Break On This Error] var index = $('li').index($('li:contains("Jerry")'); :(
Ah, sorry, fixed it. Missing a parenthesis.
3

Somewhat more efficiently:

var jerry = $('li:contains("Jerry")');
var jerry_index = jerry.siblings().index(jerry);

1 Comment

You'll have to change it to jerry.parent().children().index(jerry); though, since .index works on a set of elements.

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.