content
content copied to clipboard
Issue with "Node.nodeName": (script example not working)
MDN URL: https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName
What information was incorrect, unhelpful, or incomplete?
The short script provided does not work and yield the following error in the console under Firefox v95.0.1 (64-bit):
Uncaught TypeError: invalid assignment to const 'node'
<anonymous> https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Web/API/Node/nodeName/_sample_.Example.html:60
After testing, it does not work either under Firefox Developer Edition v96.0b6 (64-bit) and also under Chromium Version 87.0.4268.0.
This looks like it is due to the re-use of the constant node.
The script is:
const node = document.getElementsByTagName("body")[0].firstChild;
let result = "Node names are:<br/>";
while (node) {
result += node.nodeName + "<br/>";
node = node.nextSibling
}
const output = document.getElementById("result");
output.innerHTML = result;
However, if the first line is changed from const to let, then it works properly:
let node = document.getElementsByTagName("body")[0].firstChild;
Specific section or headline?
What did you expect to see?
Node names are:
#text
DIV
#text
#comment
#text
SPAN
#text
BR
#text
svg
HR
#text
OUTPUT
#text
SCRIPT
Did you test this? If so, how?
MDN Content page report details
- Folder:
en-us/web/api/node/nodename - MDN URL: https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName
- GitHub URL: https://github.com/mdn/content/blob/main/files/en-us/web/api/node/nodename/index.md
- Last commit: https://github.com/mdn/content/commit/422c1e46f09bb860015396fbecb6ef4c27cb8004
- Document last modified: 2021-11-08T14:59:08.000Z
Duplicate of #11288
- Raised a fix in #11291