Obter o número nésimo filho de um elemento em JavaScript puro
Estou criando uma função para o meu site, onde defino um atributo de dados que contém o número enésimo filho desse elemento.
Minha marcação HTML:
<html>
<body>
<section class="hardware">some text, nth-child is one</section>
<section class="hardware">some text, nth-child is two</section>
<section class="hardware">some text, nth-child is three</section>
<section class="hardware">some text, nth-child is four</section>
<section class="hardware">some text, nth-child is five</section>
</body>
</html>
Meu JavaScript até agora:
var selector = document.getElementsByClassName('hardware');
for(var i = 0; i <= selector.length; i++) {
var index = selector[i] //get the nth-child number here
selector[i].dataset.number = index;
}
Como posso obter o número enésimo filho de um elemento com JavaScript puro (não jQuery), isso é possível em JavaScript?