To find an element’s index In container with JavaScript, we use the spread operator and the indexOf
method.
For instance, we write
const index = [...el.parentElement.children].indexOf(el);
to spread the el.parentElement.children]
node list into an array with the spread operator.
Then we call indexOf
with el
to find the index of el
in the array.