The task is to iterate over all the DOM elements in efficient way. Here are few of the mostly techniques discussed with the help of JavaScript.
Approach 1:
- Use document.getElementsByTagName(‘*’) method to select all DOM elements of the document.
- Run a loop and access them one by one by indexing (Eg. el[i] for ith element).
Example: This example implements the above approach.
<!DOCTYPE HTML> <html> <head> <title> Efficient way to iterate over all DOM elements </title> </head> <body style="text-align:center;"> <h1 style="color: green"> GeeksForGeeks </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> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Approach 2:
- Use $(“*”) selector to select all DOM elements of the document.
- Change any property of element by applying it on selector.
Example: This example implements the above approach.
<!DOCTYPE HTML> <html> <head> <title> Efficient way to iterate over all DOM elements </title> <script src= </script> </head> <body style="text-align:center;"> <h1 style="color: green"> GeeksForGeeks </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> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to iterate over a JavaScript object ?
- How to iterate over the keys and values with ng-repeat in AngularJS ?
- How to iterate over filtered (ng-repeat filter) collection of objects in AngularJS ?
- How to iterate through all selected elements into an array ?
- How to iterate through child elements of a div using jQuery ?
- Iterate associative array using foreach loop in PHP
- Ways of iterating over a array in JavaScript.
- What is the Efficient way to insert a number into a sorted array of numbers in JavaScript?
- How to overlay one div over another div using CSS
- How to make the cursor to hand when a user hovers over a list item using CSS?
- Advantage of CSS3 over CSS
- Display div element on hovering over <a> tag using CSS
- How to determine which element the mouse pointer move over using JavaScript ?
- When to use self over $this in PHP ?
- How to set one column control over height in Bootstrap ?
- How to Change Background Color of a Div on Mouse Move Over using JavaScript ?
- How to translate an image or icon by hovering over it?
- How to split text horizontally on mouse move over using CSS ?
- What is the difference between of using Restangular over ngResource ?
- How to skip over an element in .map() ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


