Andrew Andrew - 12 days ago 5
Javascript Question

Javascript `nextElementSibling` from comment undefined in IE

Example: https://jsfiddle.net/theandybob/6wuc39d9/

When using IE, if I have a comment and try to access the

nextElementSibling
property from it, I get
undefined
. However, if I try to access
nextElementSibling
from something not a comment, I get my expected value. This behavior is only on IE, and I'm having a hard time trying to figure out why.

I've Googled around quite a bit, and unless I'm not searching for the right terms, I can't seem to find an answer. Any help would be appreciated.

<div id="test">
<!--anchor-->
<p>
Next Element Sibling is undefined
</p>
</div>

<div id="test2">
<span></span>
<p>
Next Element Sibling is found
</p>
</div>

Answer

It's not just IE, it's older browsers in general. nextElementSibling was a property of Elements only, not of comment nodes. Only recently the interface was changed to make the property available on other kinds of nodes as well.

Comments