== .getSiblings==

.getSiblings()

Get a list of the Element’s siblings, 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:
* The resulting list will exclude the item that it was called. * This method 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') .getSiblings() .forEach( (item) => { … }); By using a filtering function, e.g. to only collect siblings which are ''[[mdn>Web/HTML/Element/a|]]'' elements: let list = document.getElementById('foo') .getSiblings( 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.
===== More information ===== * [[mdn>Web/API/Node/nextSibling|Node: nextSibling property]] on MDN.