== .getDescendants==

.getDescendants()

Get a list of the Element’s descendants (children and children’s children), optionally filtered by a callback function.
Usage:
[[mdn>Web/JavaScript/Reference/Global_Objects/Array|Array]] = Element''.getSiblings''(callback = ''undefined'')
Member of:
[[mdn>Web/API/HTMLElement|HTMLElement]]
Parameters:
callback – a [[mdn>Web/JavaScript/Reference/Global_Objects/Function|Function]] that returns ''true'', if an item should be added (optional ).
Returns:
An [[mdn>Web/JavaScript/Reference/Global_Objects/Array|Array]] of [[mdn>Web/API/HTMLElement|HTMLElement]]s.
Notes:
* This function only returns [[mdn>Web/API/HTMLElement|Element]] nodes and ignores all other node types (like text, comments, etc.) * This function is guaranteed to return an [[mdn>Web/JavaScript/Reference/Global_Objects/Array|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 ''[[mdn>Web/HTML/Element/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 [[mdn>Web/JavaScript/Reference/Functions/Arrow_functions|arrow function expression]] to keep the code lean.
===== See also ===== * [[toolbox:traversal:getancestors:index|.getAncestors()]] * [[toolbox:traversal:getchildren:index|.getChildren()]] * [[toolbox:traversal:getsiblings:index|.getSiblings()]] ===== More information ===== * [[mdn>Web/API/Node/childNodes|Node: childNodes property]] on MDN.