plainJS

Functions / Traversing

Select the children of an element

jQuery $.children()

2015 page

This is the original page from 2015, kept at its address while the tested rewrite is in progress. The code is of its time: it ran in Internet Explorer and does not use what browsers have gained since. The rewrite replaces it here.

In short

Getting the children of a DOM element.

Selecting the children of an element with DOM methods:

js
var el = document.querySelector('div');

var children = el.childNodes,
    number_of_children = children.length;

for (var i=0; i<number_of_children; i++)
    console.log(children[i].innerHTML);

There are faster methods for only fetching the first or last child of an element:

js
var firstChild = el.firstChild;
var lastChild = el.lastChild;