Get all siblings of an element

Return the other element children of the element’s parent.

JavaScriptBrowser · DOM Version 1.0.0 · MIT · .md

get-all-siblings-of-an-element.js
function siblings(element) { return element.parentElement ? [...element.parentElement.children].filter(child => child !== element) : []; }

Usage

Markup

fixture.html
<ul><li id="a">A</li><li id="b">B</li><li id="c">C</li></ul>

Script

usage.js
siblings(document.querySelector('#b')).map(element => element.id); // → ['a', 'c']
siblings(document.createElement('li')); // → []

Lines marked → were executed in headless Chromium against the markup above, and each result was compared with the value shown. Lines marked e.g. are illustrative.

Know the limits

  • Element siblings only: text and comment nodes are skipped. The original threw a TypeError for an element without a parent; this version returns [].

The function above is the code the usage lines run. It does not validate its arguments; add checks where it receives untrusted input.

Source & maintenance

Based on Get all siblings of an element from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.

Returns [] for an element without a parent instead of throwing. Executed in Chromium. Original MDX retained as inert source evidence.

Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.

Usage lines executed in headless Chromium against the fixture markup; no external services · reviewed October 28, 2026.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.