Table of Contents

<HTMLElement>.getDescendants()

Get a list of the Element’s descendants (children and children’s children), optionally filtered by a callback function.

Usage:
Array = Element.getSiblings(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.)
  • This function is guaranteed to return an Array, even if no (matching) elements were found.

Examples

A simple example could look like this:

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

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

let list = document.getElementById('foo')
  .getDescendants( 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