Saturday, November 22, 2025
HomeLanguagesJavascriptWhat are the efficient ways to iterate over all DOM elements ?

What are the efficient ways to iterate over all DOM elements ?

The task is to iterate over all the DOM elements in an efficient way. Here are a few of the techniques discussed with the help of JavaScript. Approach 1:

Example: This example implements the above approach. 

html




<body>
    <h1 style="color: green">
        neveropen
    </h1>
      
    <p id="GFG_UP"></p>
      
    <button onclick="gfg_Run()">
        Click Here
    </button>
      
    <p id="GFG_DOWN" style="color:green;"></p>
      
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
        el_up.innerHTML = "Click on the button to "
                    + "iterate over all DOM elements.";
          
        function gfg_Run() {
            var x = document.getElementsByTagName('*');
              
            for (var i = x.length; i--;) {
                x[i].style.color = "red";
            }
              
            el_down.innerHTML = "Every element has "
                        + "been traversed and color "
                        + "property has been changed.";
        }
    </script>
</body>


Output:

What are the efficient ways to iterate over all DOM elements ?

What are the efficient ways to iterate over all DOM elements ?

Approach 2:

  • Use $(“*”) selector to select all DOM elements of the document.
  • Change any property of an element by applying it to the selector.

Example: This example implements the above approach. 

html




<head>
    <script src=
    </script>
</head>
<body>
    <h1 style="color: green">
        neveropen
    </h1>
      
    <p id="GFG_UP"></p>
      
    <button onclick="gfg_Run()">
        Click Here
    </button>
      
    <p id="GFG_DOWN" style="color:green;"></p>
      
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
        el_up.innerHTML = "Click on the button to "
                    + "iterate over all DOM elements.";
          
        function gfg_Run() {
            $("*").css("color", "red");
            el_down.innerHTML = "Every element has "
                + "been traversed and color "
                + "property has been changed.";
        }
    </script>
</body>


Output:

What are the efficient ways to iterate over all DOM elements ?

What are the efficient ways to iterate over all DOM elements ?

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6784 POSTS0 COMMENTS
Nicole Veronica
11931 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11999 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6863 POSTS0 COMMENTS
Umr Jansen
6848 POSTS0 COMMENTS