NodeList vs HTMLCollection: The Difference Between Live and Static Collections

we will examine NodeList and HTMLCollection in detail and what the NodeList and HTMLCollection.

First, Both have a length property that returns the number of elements in the list (collection).

1. HTMLCollection

HTMLCollection in the HTM…


This content originally appeared on DEV Community and was authored by Sonay Kara

we will examine NodeList and HTMLCollection in detail and what the NodeList and HTMLCollection.

First, Both have a length property that returns the number of elements in the list (collection).

1. HTMLCollection

HTMLCollection in the HTML DOM is live; getElementsByClassName() or getElementsByTagName() returns a live HTMLCollection representing an array-like object of all child elements which have all of the given class names.

Example :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NodeList and HTMLCollection</title>
</head>
<body>
      <ul class="items">
        <li>item-1</li>
        <li>item-2</li>
        <li>item-3</li>
        <li>item-4</li>
      </ul>
      <ul class="items">
        <li>item-5</li>
        <li>item-6</li>
        <li>item-7</li>
        <li>item-8</li>
      </ul>
      <ul class="items">
        <li>item-9</li>
        <li>item-10</li>
        <li>item-11</li>
        <li>item-12</li>
      </ul>
    <script src="/script.js"></script>
</body>
</html>

const selected = document.getElementsByClassName("items")
console.log(selected)

Output :

Image description

HTMLCollection is automatically updated when the underlying document is changed.

Let's write an example :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NodeList and HTMLCollection</title>
</head>
<body>
    <div class="card" >
        <div class="card-body" id="main">
          <h5 class="card-title">dev community</h5>
          <p class="card-text">dev community sonay kara</p>
          <a href="#" class="btn btn-primary">dev community sonay kara</a>
        </div>
      </div>

    <script src="/script.js"></script>
</body>
</html>

const selected = document.getElementsByClassName("card")
console.log(selected)
selected[0].innerHTML += `<li class="card">dev.to</li>`;
console.log(selected)

Output :

Image description

As can be seen from the output, when a new HTML tag is added to the element with the card class, the HTMLCollection is updated because it is live

2. NodeList

querySelectorAll() returns a static (non live) NodeList representing a list of the document's elements that match the specified group of selectors. but childNodes return a live NodeList.

Example :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NodeList and HTMLCollection</title>
</head>
<body>
      <ul class="items">
        <li>item-1</li>
        <li>item-2</li>
        <li>item-3</li>
        <li>item-4</li>
      </ul>
      <ul class="items">
        <li>item-5</li>
        <li>item-6</li>
        <li>item-7</li>
        <li>item-8</li>
      </ul>
      <ul class="items">
        <li>item-9</li>
        <li>item-10</li>
        <li>item-11</li>
        <li>item-12</li>
      </ul>
    <script src="/script.js"></script>
</body>
</html>

const selected = document.querySelectorAll(".items")
console.log(selected)

Output :

Image description

The NodeList returned by querySelectorAll() is not automatically updated when changes are made to the underlying document because its non live.

Let's write an example :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NodeList and HTMLCollection</title>
</head>
<body>
    <div class="card" >
        <div class="card-body" id="main">
          <h5 class="card-title">dev community</h5>
          <p class="card-text">dev community sonay kara</p>
          <a href="#" class="btn btn-primary">dev community sonay kara</a>
        </div>
      </div>

    <script src="/script.js"></script>
</body>
</html>

const selected = document.querySelectorAll(".card")
selected[0].innerHTML += `<li class="card">dev.to</li>`;
console.log(selected)

Output :

  • Browser

Image description

  • Console

Image description

As can be seen from the outputs, when a new HTML tag is added to the element with the card class, the browser updates, but the NodeList is not updated because NodeList is not live.

The NodeList returned by childNodes is automatically updated when changes are made to the underlying document because its live.

Example :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NodeList and HTMLCollection</title>
</head>
<body>
    <div class="card" >
        <div class="card-body" id="main">
          <h5 class="card-title">dev community</h5>
          <p class="card-text">dev community sonay kara</p>
          <a href="#" class="btn btn-primary">dev community sonay kara</a>
        </div>
      </div>

    <script src="/script.js"></script>
</body>
</html>
const selected = document.querySelector(".card")
selected.innerHTML += `<li class="card">dev.to</li>`;
console.log(selected.childNodes)

Output :

Image description

As can be seen from the output, when a new HTML tag is added to the element with the card class, the NodeList is updated because it is live.

Conclusion

In conclusion, an HTMLCollection is always a live collection. A NodeList is most often a static collection.

We examined what NodeList and HTMLCollection are. Now you know what NodeList and HTMLCollection are.


This content originally appeared on DEV Community and was authored by Sonay Kara


Print Share Comment Cite Upload Translate Updates
APA

Sonay Kara | Sciencx (2024-11-05T19:20:13+00:00) NodeList vs HTMLCollection: The Difference Between Live and Static Collections. Retrieved from https://www.scien.cx/2024/11/05/nodelist-vs-htmlcollection-the-difference-between-live-and-static-collections/

MLA
" » NodeList vs HTMLCollection: The Difference Between Live and Static Collections." Sonay Kara | Sciencx - Tuesday November 5, 2024, https://www.scien.cx/2024/11/05/nodelist-vs-htmlcollection-the-difference-between-live-and-static-collections/
HARVARD
Sonay Kara | Sciencx Tuesday November 5, 2024 » NodeList vs HTMLCollection: The Difference Between Live and Static Collections., viewed ,<https://www.scien.cx/2024/11/05/nodelist-vs-htmlcollection-the-difference-between-live-and-static-collections/>
VANCOUVER
Sonay Kara | Sciencx - » NodeList vs HTMLCollection: The Difference Between Live and Static Collections. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/11/05/nodelist-vs-htmlcollection-the-difference-between-live-and-static-collections/
CHICAGO
" » NodeList vs HTMLCollection: The Difference Between Live and Static Collections." Sonay Kara | Sciencx - Accessed . https://www.scien.cx/2024/11/05/nodelist-vs-htmlcollection-the-difference-between-live-and-static-collections/
IEEE
" » NodeList vs HTMLCollection: The Difference Between Live and Static Collections." Sonay Kara | Sciencx [Online]. Available: https://www.scien.cx/2024/11/05/nodelist-vs-htmlcollection-the-difference-between-live-and-static-collections/. [Accessed: ]
rf:citation
» NodeList vs HTMLCollection: The Difference Between Live and Static Collections | Sonay Kara | Sciencx | https://www.scien.cx/2024/11/05/nodelist-vs-htmlcollection-the-difference-between-live-and-static-collections/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.