"); ``` ### Output ```text Node Name: bookstore (Node Type: 1) ``` --- ## Related Properties * **`firstChild`**: Returns the first child node of a specified node. * **`childNodes`**: Returns a collection of a node's child nodes (as a `NodeList`). * **`previousSibling`**: Returns the node immediately preceding the specified node at the same tree level. * **`nextSibling`**: Returns the node immediately following the specified node at the same tree level.
Dom Prop Node Lastchild
# XML DOM: lastChild Property
The `lastChild` property is a standard property of the XML DOM `Node` object. It is used to retrieve the last child node of a specified node.
---
## Definition and Usage
The `lastChild` property returns the last child node of the selected node as a `Node` object. If the specified node has no children, this property returns `null`.
### Syntax
```javascript
nodeObject.lastChild
```
### Return Value
* **Node Object**: Represents the last child of the specified node.
* **null**: Returned if the node has no child nodes.
---
## Important Considerations: Whitespace and Text Nodes
When working with the DOM, it is crucial to understand how different environments handle whitespace (such as spaces, tabs, and line breaks) between elements:
* **Standard Browsers (Firefox, Chrome, Safari, Edge)**: Treat whitespace or line breaks between XML/HTML tags as empty **Text Nodes** (with a `nodeType` of 3).
* **Legacy Browsers (such as older versions of Internet Explorer)**: Ignore these whitespace text nodes and only consider element nodes.
Because of this discrepancy, calling `nodeObject.lastChild` directly might return a text node containing empty spaces or line breaks instead of the actual XML/HTML element you expect.
### Solution: Filtering for Element Nodes
To ensure cross-browser compatibility and reliably target the last **Element Node** (which has a `nodeType` of `1`), you can implement a helper function that traverses backward using the `previousSibling` property until it finds an element node.
---
## Code Example
The following example demonstrates how to load an XML document (`books.xml`) and retrieve its last child node safely across all browsers.
### The XML Structure (`books.xml`)
Assume our XML file looks like this:
```xml
Everyday Italian
Giada De Laurentiis
2005
30.00
```
### JavaScript Implementation
```javascript
// Helper function to get the last child element node (nodeType === 1)
function get_lastchild(n) {
var x = n.lastChild;
// Loop backwards until we find a node with nodeType 1 (Element)
while (x && x.nodeType !== 1) {
x = x.previousSibling;
}
return x;
}
// Load the XML document
var xmlDoc = loadXMLDoc("books.xml");
// Retrieve the last child element of the document
var lastElement = get_lastchild(xmlDoc);
// Output the results
document.write("Node Name: " + lastElement.nodeName);
document.write(" (Node Type: " + lastElement.nodeType + ")
"); ``` ### Output ```text Node Name: bookstore (Node Type: 1) ``` --- ## Related Properties * **`firstChild`**: Returns the first child node of a specified node. * **`childNodes`**: Returns a collection of a node's child nodes (as a `NodeList`). * **`previousSibling`**: Returns the node immediately preceding the specified node at the same tree level. * **`nextSibling`**: Returns the node immediately following the specified node at the same tree level.
"); ``` ### Output ```text Node Name: bookstore (Node Type: 1) ``` --- ## Related Properties * **`firstChild`**: Returns the first child node of a specified node. * **`childNodes`**: Returns a collection of a node's child nodes (as a `NodeList`). * **`previousSibling`**: Returns the node immediately preceding the specified node at the same tree level. * **`nextSibling`**: Returns the node immediately following the specified node at the same tree level.
YouTip