As per the jQuery api, the complementary operation to .get(), which accepts an index and returns a DOM node, .index()
can take a DOM node and returns an index.
No loops needed, call Array#indexOf
on .parentElement.children
:
const element = document.querySelector('#baz');
[].indexOf.call(element.parentElement.children, element);
// => 2
You can even call it on a random list of elements, just like you can in jQuery:
const list = document.querySelectorAll('li');
const element = document.querySelector('#baz');
[].indexOf.call(list, element);
// => 2