How to find an element’s index In container with JavaScript?

Spread the love

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.

Leave a Reply

Your email address will not be published. Required fields are marked *