If I got a parent node, how can I loop through every third child node?
I've got now this code:
var parents = document.getElementById('ID_of_parent');
var first_child = parents.firstChild.data.id;
alert(parents);
alert(first_child);
For the parents, i got now '[object HTMLDivElement]' and for first_child i got 'undefined'.
var nodes = document.getElementById('ID_of_parent').childNodes;
for(i=0; i<nodes.length; i+=3) {
alert(nodes[i]);
}
Have you consider jQuery?
$("#ID_of_parent > *:nth-child(3n)").each(function() { alert(this);});
I implemented a demo here: http://jsbin.com/ahije4/5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With