Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the index of the element in javascript?

Tags:

The NodeList don't have a indexOf(element) method? So, how can I get the element index?

like image 942
The Student Avatar asked Sep 23 '10 15:09

The Student


People also ask

How do you get the index of an item in a list JavaScript?

The indexOf() method returns the first index (position) of a specified value. The indexOf() method returns -1 if the value is not found. The indexOf() method starts at a specified index and searches from left to right. By default the search starts at the first element and ends at the last.

How do you find the index of an element?

indexOf() The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

Can we find index of object in JavaScript?

To find the index of an object in an array, by a specific property: Use the map() method to iterate over the array, returning only the value of the relevant property. Call the indexOf() method on the returned from map array. The indexOf method returns the index of the first occurrence of a value in an array.

What is index of in JavaScript?

indexOf() The indexOf() method, given one argument: a substring to search for, searches the entire calling string, and returns the index of the first occurrence of the specified substring.


1 Answers

You can use Array.prototype.indexOf.call() like this

let nodes = document.getElementsByTagName('*'); Array.prototype.indexOf.call(nodes, document.body); 
like image 79
kennebec Avatar answered Sep 20 '22 13:09

kennebec