"); } ``` --- ## Key Considerations 1. **Live vs. Static NodeLists:** * Methods like `getElementsByTagName()` return a **live** `NodeList`. If the DOM structure changes (e.g., elements are added or removed), the `length` property will automatically update to reflect the current state of the document. * Methods like `querySelectorAll()` return a **static** `NodeList`. Changes in the DOM will not affect its `length` or contents. 2. **Zero-Based Indexing:** The index of a `NodeList` starts at `0` and ends at `length - 1`. Attempting to access `nodelistObject[nodelistObject.length]` will return `undefined`. 3. **Read-Only:** The `length` property is read-only. You cannot manually resize a `NodeList` by assigning a new value to its `length` property.
Dom Prop Nodelist Length
## XML DOM NodeList length Property
The `length` property of the `NodeList` object returns the number of nodes currently present in a node list. This property is essential for determining the size of a collection of elements and is commonly used to loop through DOM nodes.
---
## Syntax
```javascript
nodelistObject.length
```
### Return Value
* **Type:** `Number`
* **Description:** An integer representing the total number of nodes in the `NodeList`.
---
## Code Examples
### Example 1: Getting the Count of Specific Elements
The following code snippet loads an XML document (`books.xml`) and retrieves the total number of `` elements in the document.
```javascript
// Load the XML document
const xmlDoc = loadXMLDoc("books.xml");
// Retrieve all elements as a NodeList
const titleList = xmlDoc.getElementsByTagName('title');
// Output the length of the NodeList
document.write("Number of title elements: " + titleList.length);
```
#### Expected Output:
```text
Number of title elements: 4
```
---
### Example 2: Iterating Through a NodeList Using `length`
The most common use case for the `length` property is to safely loop through all nodes in a collection.
```javascript
// Load the XML document
const xmlDoc = loadXMLDoc("books.xml");
// Retrieve all elements
const titleList = xmlDoc.getElementsByTagName('title');
// Loop through the NodeList using its length property
for (let i = 0; i < titleList.length; i++) {
document.write(titleList.childNodes.nodeValue + "
"); } ``` --- ## Key Considerations 1. **Live vs. Static NodeLists:** * Methods like `getElementsByTagName()` return a **live** `NodeList`. If the DOM structure changes (e.g., elements are added or removed), the `length` property will automatically update to reflect the current state of the document. * Methods like `querySelectorAll()` return a **static** `NodeList`. Changes in the DOM will not affect its `length` or contents. 2. **Zero-Based Indexing:** The index of a `NodeList` starts at `0` and ends at `length - 1`. Attempting to access `nodelistObject[nodelistObject.length]` will return `undefined`. 3. **Read-Only:** The `length` property is read-only. You cannot manually resize a `NodeList` by assigning a new value to its `length` property.
"); } ``` --- ## Key Considerations 1. **Live vs. Static NodeLists:** * Methods like `getElementsByTagName()` return a **live** `NodeList`. If the DOM structure changes (e.g., elements are added or removed), the `length` property will automatically update to reflect the current state of the document. * Methods like `querySelectorAll()` return a **static** `NodeList`. Changes in the DOM will not affect its `length` or contents. 2. **Zero-Based Indexing:** The index of a `NodeList` starts at `0` and ends at `length - 1`. Attempting to access `nodelistObject[nodelistObject.length]` will return `undefined`. 3. **Read-Only:** The `length` property is read-only. You cannot manually resize a `NodeList` by assigning a new value to its `length` property.
YouTip