DEV Community

Discussion on: JavaScript Document Object (DOM) Helpers

Collapse
 
ironydelerium profile image
ironydelerium

export function siblings(el, sel = '*') {
  let r = [...el.parentNode.querySelectorAll(`:scope > ${sel}`)];
  let p = r.indexOf(el);
  if (p != -1) r.splice(p, 1);
  return r;
}

Let the browser do the work. :scope in QSA refers to the node you're calling it on; siblings excludes el; and finally, a selector other than a simple selector won't match anything anyway because:

<ul>
  <li>first</li>
  <li class='second'>second</li>
</ul>

In jQuery, $('li.second').siblings('ul > li') would return an empty set - li.second doesn't have any ul siblings, nor are it's descendants siblings of it, so supporting anything but a simple selector is moot.