JQuery `index ()` equivalent in Vanilla JS

I have a code snippet below (the currentUser class is in a different list item depending on who is viewing the page).

 <ul> <li>user 1</li> <li>user 2</li> <li class="currentUser">user 3</li> <li>user 4</li> </ul> var curLth = jQuery('.currentUser').index(); console.log(curLth); //outputs 2 

The site I'm working on doesn't load jQuery, so I want to know which list item the currentUser class currentUser without using jQuery

I checked the NodeList in dev tools, but did not see anything that I could use to get it.

How can this be achieved?

+5
source share
1 answer

Here is the equivalent:

 var curUser = document.getElementsByClassName("currentUser")[0]; var curLth = [].slice.call(curUser.parentNode.children).indexOf(curUser); console.log(curLth); //outputs 2 
+11
source

Source: https://habr.com/ru/post/1212254/


All Articles