HTML DOM Element nodeValue attribute
- Previous Page nodeType
- Next Page normalize()
- Go Back to the Previous Level HTML DOM Elements Object
Definition and Usage
nodeValue
The property sets or returns the value of the node.
If the node is an element node, then nodeValue
The property will return null
.
Note:If you want to return the text of the element, remember that the text is always located within a Text node, and you must return the node value of the Text node:
element.childNodes[0].nodeValue
For other node types,nodeValue
The property will return different values for different node types.
Alternative Solution:
See Also:
Example
Example 1
Returns the node value of the first <button> element in the document:
document.getElementsByTagName("BUTTON")[0].childNodes[0].nodeValue;
Example 2
Get the node name, value, and type of the first child of the element "myDIV":
const x = document.getElementById("myDIV").firstChild; let text = ""; text += "Name: " + x.nodeName + "<br>"; text += "Value: " + x.nodeValue + "<br>"; text += "Type: " + x.nodeType;
Syntax
Return node value:
node.nodeValue
Set node value:
node.nodeValue = value
Attribute Value
Value | Description |
---|---|
value | Node Value. |
Return Value
Type | Description |
---|---|
String |
Node Value.
|
Browser Support
element.nodeValue
It is a DOM Level 1 (1998) feature.
All browsers fully support it:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | 9-11 | Support | Support | Support | Support |
- Previous Page nodeType
- Next Page normalize()
- Go Back to the Previous Level HTML DOM Elements Object