# HTMLCollection and NodeList

```
<div id="myContent">
    <p>First paragraph</p>
    <p>Second paragraph</p>
    <p>Third paragraph</p>
    <p>Fourth paragraph</p>
</div>
```

With the above HTML structure, the DOM API `document.getElementsByTagName("p")` will return an array-like list called `HTMLCollection` while `document.querySelectorAll("p")` will also return an **array-like list** called `NodeList`.

| DOM API                                | Result                           |
| -------------------------------------- | -------------------------------- |
| **document.getElementsByTagName("p")** | ![](/files/-LES_TPaxIlQJS80-iaP) |
|                                        |                                  |
| **document.querySelectorAll("p")**     | ![](/files/-LES_TPlMdbZJ2W-ep9j) |

Although the contents of both lists are same, `HTMLCollection`is always a **live list** - if the corresponding DOM gets updated, so will the HTMLCollection.

```javascript
var myContent = document.querySelector("#myContent");
var myHTMLCollection = myContent.getElementsByTagName("p");
var myNodeList = myContent.querySelectorAll("p");

console.log(myHTMLCollection.length);    // 4
console.log(myNodeList.length);          // 4

// Adding new paragraph to #myContent
var newParagraph = document.createElement("p");
myContent.appendChild(newParagraph);

console.log(myHTMLCollection.length);    // 5 - live list reflects the current state of DOM
console.log(myNodeList.length);          // 4
```

Further, it is also possible to get hold of a live NodeList; e.g. `{HTMLElement}.childNodes`.

Today, the concept of HTMLCollection probably exists in modern browser implementations just for legacy purposes - to retain the behavior of older and now non-standard DOM APIs like `document.forms`, `HTMLElement.children`, `document.getElementsByTagName()` etc. But this is all browser implementation specific. Not every browser will follow same implementation.

## A word of caution about live collections

As an example of live collections, `{HTMLElement}.childNodes` returns a **live** `NodeList` and `document.getElementsByTagName()` returns a **live** `HTMLCollection`. One should be extra careful while iterating on these entities, as an operation that updates the same collection from within the iteration might affect the length of the loop in an unexpected way.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://short-js.webf.zone/dom/htmlcollection-and-nodelist.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
