Find when an element comes in a list of elements

How do I find the order of an element like when it appears in a list of elements.
Do I have to name them by their order. Or is there a programmatic way of doing it?
Thanks! :slight_smile:

Do you mean the index of a value in an array? There are a few array methods that can help with that.

Are you working through the curriculum? We go over this one there!

No, not right now.

I think I didn’t explain my question properly.

I have a parent container.

div.parent
And some elements in it :-
div.child
div.child
div.child
div.child

I want to know if there is a way to pick the 3rd child without giving it the id of “third” and targeting it that way in javascript

Like…I don’t know…
parent.getnthchild(3) to get the third child.

Thank you very much!

Ahhhh the css selector. Something like this? DevDocs

.parent div.child:nth-child(2){
  border: 3px solid goldenrod;
  background-color: lilac;
}

And the same thing works in javascript:

const myFavoriteElement = document.querySelector(".container :nth-child(4)" );
1 Like

OOO. I knew, I used to know it. I haven’t worked in javascript for quite a while. IDK how I could forget a thing that I used so often.
Thank you so much, this is exactly what I was looking for.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.