JavaScript: How to loop through all DOM elements on a page

In JavaScript, if you need to read or manipulate all elements, you can pass a * to the getElementsByTagName method so it will return all elements in a page.

let your_variable = document.getElementsByTagName("*");

for (let i=1; i<your_variable.length; i++){
                // Do something with the element here
            }

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.