How to find all Siblings of the currently selected DOM object

dom, javascript

Solution

I'll assume that this takes place inside an event handler where `this` is a reference to the targeted element whose siblings you want to affect.

If not, adjustments will be needed.

var result = [],
    node = this.parentNode.firstChild;

while ( node ) {
    if ( node !== this && node.nodeType === Node.ELEMENT_NODE ) 
      result.push( node );
    node = node.nextElementSibling || node.nextSibling;
}

// result will contain all type 1 siblings of "this"

Problem

What is the perfect way to find all nextSiblings and previousSiblings in JavaScript. I tried few ways but not getting accurate solution. If any element is selected, I need to get length of all next siblings excluding white-space, any spaces or line-breaks. Also I don't want to use jQuery for this. I am specifically looking something from JavaScript

Original source

Related problems