8

I have the below code snippet (currentUser class is on 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 am working on does not load jQuery, so I want to know which list item has the class currentUser without using jQuery

I have inspected the NodeList in the dev tools but haven't seen anything that I can use to get this.

How can this be achieved?

Badacadabra
  • 8,043
  • 7
  • 28
  • 49
ak85
  • 4,154
  • 18
  • 68
  • 113
  • 3
    possible duplicate of [jQuery .index() in javascript](http://stackoverflow.com/questions/13658021/jquery-index-in-javascript) – artm Jan 30 '15 at 05:13
  • @artm thanks for that link. I was having a look around but obviously was using the best search terms – ak85 Jan 30 '15 at 05:46

1 Answers1

18

Here is the equivalant:

var curUser = document.getElementsByClassName("currentUser")[0];
var curLth = [].slice.call(curUser.parentNode.children).indexOf(curUser);
console.log(curLth); //outputs 2
Vadim
  • 1,125
  • 8
  • 18