Skip to content

Incorrect usage of nextSibling and previousSibling #3771

@Obrienzo

Description

@Obrienzo

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions