javascript-tutorial / javascript-tutorial/en.javascript.info

Incorrect usage of nextSibling and previousSibling

Open
#3,771 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
25.5k
Forks
4k
PR merge metrics
No merged PRs in 30d

Description

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!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Walking the DOM page at the linked documentation URL, specifically the “Siblings and the parent” section and its nextSibling and previousSibling examples. Check that the examples accurately account for text nodes and update the wording or code so the expected results are clear. Done means the examples no longer imply that these properties always return HTMLBodyElement or HTMLHeadElement.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.