Table of Contents

<HTMLElement>.getChildren()

Get a list of the Element’s immediate child elements, optionally filtered by a callback function.

Usage:
Array = Element.getChildren(callback = undefined)
Member of:
HTMLElement
Parameters:
callback – a Function that returns true, if an item should be added (optional ).
Returns:
An Array of HTMLElements.
Notes:
  • This function only returns Element nodes and ignores all other node types (like text, comments, etc.)

Examples

A simple example could look like this:

document.getElementById('foo')
  .getChildren()
  .forEach( (item) => {});

By using a filtering function, e.g. to only collect descendants which are <a> elements:

let list = document.getElementById('foo')
  .getChildren( e => e.nodeName == 'A' );
 
Notes:
  • The elements’ .nodeName property always contains the element name in uppercase letters!
  • The above example uses an arrow function expression to keep the code lean.

See also

More information