YouTip LogoYouTip

Prop Attr Nodevalue

# XML DOM: Attr nodeValue Property The `nodeValue` property is a core part of the XML DOM (Document Object Model) interface. When used on an **Attribute (Attr) node**, it allows you to retrieve or modify the value of that specific attribute. --- ## Definition and Usage The `nodeValue` property sets or returns the value of a node, depending on its node type. For an **Attribute node** (which has a `nodeType` of `2`), the `nodeValue` property returns the text value assigned to the attribute. You can also use this property to dynamically update the attribute's value. --- ## Syntax ### Get the attribute value: ```javascript attrObject.nodeValue ``` ### Set the attribute value: ```javascript attrObject.nodeValue = "newValue" ``` ### Return Value * **Type:** String * **Description:** The text value of the attribute. --- ## Practical Example The following example demonstrates how to load an XML document (`books.xml`) using `loadXMLDoc()` and iterate through the `` elements to display the name, value, and type of their first attribute. ### Sample XML File (`books.xml`) ```xml Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter J K. Rowling 2005 29.99 Learning XML Erik T. Ray 2003 39.95 ``` ### JavaScript Implementation ```javascript // Load the XML document var xmlDoc = loadXMLDoc("books.xml"); // Get all elements var books = xmlDoc.getElementsByTagName('book'); for (var i = 0; i < books.length; i++) { // Access the first attribute of the current book element var attribute = books.item(i).attributes; // Output the attribute's name, value, and nodeType document.write(attribute.nodeName); document.write(" = "); document.write(attribute.nodeValue); document.write(" (nodetype: " + attribute.nodeType + ")"); document.write("
"); } ``` ### Output ```text category = COOKING (nodetype: 2) category = CHILDREN (nodetype: 2) category = WEB (nodetype: 2) ``` --- ## Key Considerations ### 1. Node Types and `nodeValue` Behavior The behavior of `nodeValue` varies significantly depending on the type of DOM node you are targeting: | Node Type | Node Name (`nodeName`) | Node Value (`nodeValue`) | | :--- | :--- | :--- | | **Attribute (Attr)** | Attribute Name | **Attribute Value** | | **Element** | Tag Name | `null` | | **Text** | `#text` | Content of the text node | > **Note:** If you try to call `nodeValue` directly on an **Element node** (e.g., ``), it will return `null`. To get the text inside an element, you must access its child text node (`element.childNodes.nodeValue`) or use modern properties like `textContent`. ### 2. Alternative Properties While `nodeValue` is highly effective for XML DOM manipulation, in modern web development with HTML documents, developers often use alternative properties for convenience: * **`value`**: The `Attr` interface also provides a direct `.value` property (e.g., `attribute.value`), which functions identically to `nodeValue` for attributes. * **`getAttribute()` / `setAttribute()`**: Instead of navigating through the `.attributes` named node map, you can directly read or write attributes on an element using `element.getAttribute("category")` and `element.setAttribute("category", "newValue")`.
← Prop Attr OwnerdocumentProp Attr Nodetype β†’