NodeList and HTMLCollection : Live collection and Static collection

NodeList and HTMLCollection : Live collection and Static collection


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 :


 lang="en">

     charset="UTF-8">
     name="viewport" content="width=device-width, initial-scale=1.0">
    </span>NodeList and HTMLCollection<span class="nt"/>
<span class="nt"/>
<span class="nt"/>
      <span class="nt"><ul> <span class="na">class=</span><span class="s">"items"</span><span class="nt">></span>
        <span class="nt"><li/></span>item-1<span class="nt"/>
        <span class="nt"><li/></span>item-2<span class="nt"/>
        <span class="nt"><li/></span>item-3<span class="nt"/>
        <span class="nt"><li/></span>item-4<span class="nt"/>
      <span class="nt"/></ul></span>
      <span class="nt"><ul> <span class="na">class=</span><span class="s">"items"</span><span class="nt">></span>
        <span class="nt"><li/></span>item-5<span class="nt"/>
        <span class="nt"><li/></span>item-6<span class="nt"/>
        <span class="nt"><li/></span>item-7<span class="nt"/>
        <span class="nt"><li/></span>item-8<span class="nt"/>
      <span class="nt"/></ul></span>
      <span class="nt"><ul> <span class="na">class=</span><span class="s">"items"</span><span class="nt">></span>
        <span class="nt"><li/></span>item-9<span class="nt"/>
        <span class="nt"><li/></span>item-10<span class="nt"/>
        <span class="nt"><li/></span>item-11<span class="nt"/>
        <span class="nt"><li/></span>item-12<span class="nt"/>
      <span class="nt"/></ul></span>
    <span class="nt"><script type="litespeed/javascript"><span class="na">src=]]></script></span><span class="s">"/script.js"</span><span class="nt">></span>
<span class="nt"/>
<span class="nt"/>

</span></span></span></code></pre><div class="highlight__panel js-actions-panel"><div class="highlight__panel-action js-fullscreen-code-action">
<svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode

Exit fullscreen mode

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

Exit fullscreen mode

Output :

Image description


HTMLCollection is automatically updated when the underlying document is changed.

Let’s write an example :


 lang="en">

     charset="UTF-8">
     name="viewport" content="width=device-width, initial-scale=1.0">
    </span>NodeList and HTMLCollection<span class="nt"/>
<span class="nt"/>
<span class="nt"/>
    <span class="nt"/>

    <span class="nt"><script type="litespeed/javascript"><span class="na">src=]]></script></span><span class="s">"/script.js"</span><span class="nt">></span>
<span class="nt"/>
<span class="nt"/>

</span></span></span></code></pre><div class="highlight__panel js-actions-panel"><div class="highlight__panel-action js-fullscreen-code-action">
<svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode

Exit fullscreen mode

const selected = document.getElementsByClassName("card")
console.log(selected)
selected[0].innerHTML += `
  • dev.to
  • `
    ; console.log(selected)
    Enter fullscreen mode

    Exit fullscreen mode

    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 :

    
     lang="en">
    
         charset="UTF-8">
         name="viewport" content="width=device-width, initial-scale=1.0">
        </span>NodeList and HTMLCollection<span class="nt"/>
    <span class="nt"/>
    <span class="nt"/>
          <span class="nt"><ul> <span class="na">class=</span><span class="s">"items"</span><span class="nt">></span>
            <span class="nt"><li/></span>item-1<span class="nt"/>
            <span class="nt"><li/></span>item-2<span class="nt"/>
            <span class="nt"><li/></span>item-3<span class="nt"/>
            <span class="nt"><li/></span>item-4<span class="nt"/>
          <span class="nt"/></ul></span>
          <span class="nt"><ul> <span class="na">class=</span><span class="s">"items"</span><span class="nt">></span>
            <span class="nt"><li/></span>item-5<span class="nt"/>
            <span class="nt"><li/></span>item-6<span class="nt"/>
            <span class="nt"><li/></span>item-7<span class="nt"/>
            <span class="nt"><li/></span>item-8<span class="nt"/>
          <span class="nt"/></ul></span>
          <span class="nt"><ul> <span class="na">class=</span><span class="s">"items"</span><span class="nt">></span>
            <span class="nt"><li/></span>item-9<span class="nt"/>
            <span class="nt"><li/></span>item-10<span class="nt"/>
            <span class="nt"><li/></span>item-11<span class="nt"/>
            <span class="nt"><li/></span>item-12<span class="nt"/>
          <span class="nt"/></ul></span>
        <span class="nt"><script type="litespeed/javascript"><span class="na">src=]]></script></span><span class="s">"/script.js"</span><span class="nt">></span>
    <span class="nt"/>
    <span class="nt"/>
    
    </span></span></span></code></pre><div class="highlight__panel js-actions-panel"><div class="highlight__panel-action js-fullscreen-code-action">
    <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode
    
    

    Exit fullscreen mode

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

    Exit fullscreen mode

    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 :

    
     lang="en">
    
         charset="UTF-8">
         name="viewport" content="width=device-width, initial-scale=1.0">
        </span>NodeList and HTMLCollection<span class="nt"/>
    <span class="nt"/>
    <span class="nt"/>
        <span class="nt"/>
    
        <span class="nt"><script type="litespeed/javascript"><span class="na">src=]]></script></span><span class="s">"/script.js"</span><span class="nt">></span>
    <span class="nt"/>
    <span class="nt"/>
    
    </span></span></span></code></pre><div class="highlight__panel js-actions-panel"><div class="highlight__panel-action js-fullscreen-code-action">
    <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode
    
    

    Exit fullscreen mode

    const selected = document.querySelectorAll(".card")
    selected[0].innerHTML += `
  • dev.to
  • `
    ; console.log(selected)
    Enter fullscreen mode

    Exit fullscreen mode

    Output :

    Image description

    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 :

    
     lang="en">
    
         charset="UTF-8">
         name="viewport" content="width=device-width, initial-scale=1.0">
        </span>NodeList and HTMLCollection<span class="nt"/>
    <span class="nt"/>
    <span class="nt"/>
        <span class="nt"/>
    
        <span class="nt"><script type="litespeed/javascript"><span class="na">src=]]></script></span><span class="s">"/script.js"</span><span class="nt">></span>
    <span class="nt"/>
    <span class="nt"/>
    </span></span></span></code></pre><div class="highlight__panel js-actions-panel"><div class="highlight__panel-action js-fullscreen-code-action">
    <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode
    
    

    Exit fullscreen mode

    const selected = document.querySelector(".card")
    selected.innerHTML += `
  • dev.to
  • `
    ; console.log(selected.childNodes)
    Enter fullscreen mode

    Exit fullscreen mode

    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.



    Source link

    Leave a Reply

    Your email address will not be published. Required fields are marked *