<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?
To access the index of an element in jQuery, use the eq() method. The eq() method refers the position of the element.
In jQuery . get() method loads data from the server by using the GET HTTP request. This method returns XMLHttpRequest object. Syntax.
Definition and Usage. The :eq() selector selects an element with a specific index number. The index numbers start at 0, so the first element will have the index number 0 (not 1). This is mostly used together with another selector to select a specifically indexed element in a group (like in the example above).
The easiest way to find an HTML element in the DOM, is by using the element id.
Using jQuery's .index
will give you the index of an element in the given elements:
var index = $('li').index($('li:contains("Jerry")'));
Somewhat more efficiently:
var jerry = $('li:contains("Jerry")');
var jerry_index = jerry.siblings().index(jerry);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With