<HTMLElement>.getSiblings()

Get a list of the Element’s siblings, 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:
  • 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 <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 arrow function expression to keep the code lean.

More information