Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
119
rated 0 times [  126] [ 7]  / answers: 1 / hits: 24501  / 9 Years ago, sun, june 28, 2015, 12:00:00

Although I seem to get strange results occasionally these seem to me to be the same so can someone describe the difference?


More From » javascript

 Answers
123

'nextSibling' returns the next Node object whereas 'nextElementSibling' returns the next Element object, so probably the real question is what is the difference between a Node & an Element?



Basically an Element is specified by an HTML Tag whereas a Node is any Object in the DOM, so an Element is a Node but a Node can also include Text Nodes in the form of whitespace, comments, text characters or line breaks. For more info on Elements vs Nodes see this Difference between Node object and Element object?



i.e Take the following DOM snippet



<div id=start></div>
Me
<p>Hi</p>


Using nextSibling you would get:



console.log(document.getElementById('start').nextSibling); // nMen
console.log(document.getElementById('start').nextSibling.nextSibling); // <p>


Whereas using nextElementSibling you would get:



console.log(document.getElementById('start').nextElementSibling);// <p>


Also nextElementSibling is IE10+, being the newer method whereas nextSibling has full browser support


[#66009] Friday, June 26, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dominickmackenziet

Total Points: 583
Total Questions: 101
Total Answers: 117

Location: Saint Lucia
Member since Wed, Feb 8, 2023
1 Year ago
dominickmackenziet questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Fri, Feb 12, 21, 00:00, 3 Years ago
;