I am making a function for my site where I set a data attribute which contains the nth-child number of that element.
My HTML markup:
<
I'm going to answer the questions with the following assumptions:
hardware classed elements are all siblings
some text, nth-child is zero
some text, nth-child is two
(I'm making these assumptions, because this is the problem I'm facing, thought it could be useful)
So the main difference is that instead of querying the elements that belong to a given class, I'm going to get the (direct) children of the body, and filter them.
Array.from(document.body.children)
.map((element, index) => ({element, index}))
.filter(({element}) => element.classList.contains('hardware'))
The resulting array will look like this:
[
{element: section.hardware, index: 0}
{element: section.hardware, index: 2}
]