en.javascript.info icon indicating copy to clipboard operation
en.javascript.info copied to clipboard

Incorrect usage of nextSibling and previousSibling

Open Obrienzo opened this issue 1 year ago • 0 comments

I encountered an error when learning about DOM navigation, in the Walking the DOM, under the Siblings and the parent using nextSibling and previousSibling. These properties return the next and previous node, which is the text node.

// parent of <body> is <html>
alert(document.body.parentNode === document.documentElement); // true

// after <head> goes <body>
alert(document.head.nextSibling); // Might not be HTMLBodyElement due to text nodes

// before <body> goes <head>
alert(document.body.previousSibling); // Might not be HTMLHeadElement due to text nodes

If you look at the alert displayed. The nextSibling and previousSibling may not return the expected HTMLBodyElement and HTMLHeadElement due to the presence of a text node.

Please consider updating, or correcting the examples.

Thank you!

Obrienzo avatar Oct 20 '24 17:10 Obrienzo