Ani Ani - 1 year ago 135
Javascript Question

What is the difference between node.nextSibling and ChildNode.nextElementSibling?

<div id="div-01">Here is div-01</div>
<div id="div-02">Here is div-02</div>

Aren't they the same thing ?

Both returning the immediately followed node. I read a lot of articles but seems to me like the same thing, but can't figure where to use one versus other ?

Answer Source

nextElementSibling always returns an element. nextSibling can return any kind of node. They are the same for your example, but different in other cases, e.g.:

<p><span id="span-01">Here is span-01</span>
Some text at the top level
<span id="span-02">Here is span-02</span></p>

In this case, document.getElementById('span-01').nextElementSibling is span-02, but document.getElementById('span-01').nextSibling is the text node containing "Some text at the top level".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download