The nodeValue property is used to get the text value of a node.
The getAttribute() method returns the value of an attribute.
In the DOM, everything is a node. Element nodes do not have a text value.
The text of an element node is stored in a child node. This node is called a text node.
The way to get the text of an element is to get the value of the child node (text node).
The getElementsByTagName() method returns a node list containing all elements with the specified tag name in the same order as they appear in the source document.
The following code loads "books.xml" into xmlDoc using loadXMLDoc() and retrieves the first <title> element:
The childNodes property returns a list of child nodes. The <title> element has only one child node. It is a text node.
The following code retrieves the text node of the <title> element:
The nodeValue property returns the text value of the text node:
Result: txt = "Everyday Italian"
Loop through all <title> elements: Try it yourself
In the DOM, attributes are nodes. Unlike element nodes, attribute nodes have text values.
The way to get the value of an attribute, is to get its text value.
This can be done using the getAttribute() method or using the nodeValue property of the attribute node.
The getAttribute() method returns an attribute value.
The following code retrieves the text value of the "lang" attribute of the first <title> element:
Result: txt = "en"
Example explained:
Loop through all <book> elements and get their "category" attributes:
Try
it yourself
The getAttributeNode() method returns an attribute node.
The following code retrieves the text value of the "lang" attribute of the first <title> element:
Result: txt = "en"
Example explained:
Loop through all <book> elements and get their "category" attributes: Try it yourself
The perfect solution for professionals who need to balance work, family, and career building.
More than 10 000 certificates already issued!
The HTML Certificate documents your knowledge of HTML.
The HTML5 Certificate documents your knowledge of advanced HTML5.
The CSS Certificate documents your knowledge of advanced CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The jQuery Certificate documents your knowledge of jQuery.
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The ASP Certificate documents your knowledge of ASP, SQL, and ADO.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
Your message has been sent to W3Schools.