- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript: How to loop through all DOM elements on a page and display result on console?
To loop through all DOM elements on a page, use document.getElementsByTagName(‘*’). Loop through its length and display the result in console as in the below code −
var tags = document.getElementsByTagName("*"); for (var i=0, max=tags.length; i < max; i++) { console.log(tags[i]); }
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initialscale=1.0"> <title>Document</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <h1>Demo</h1> <label> Enter the name: <input type="text" id="txtName"> </label> <button type="submit">Save</button> <script> var tags = document.getElementsByTagName("*"); for (var i=0, max=tags.length; i < max; i++) { console.log(tags[i]); } </script> </body> </html>
To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.
Output
This will produce the following output −
- Related Articles
- How to loop through all the iframes elements of a page using jQuery?
- Select all elements with “data-” attribute with jQuery and display on Console?
- Display a message on console while focusing on input type in JavaScript?
- Getting HTML form values and display on console in JavaScript?
- Display all the titles of JTabbedPane tabs on Console in Java
- Display the dropdown’s (select) selected value on console in JavaScript?
- Display all the values of an array in p tag on a web page with JavaScript
- How to read data from PDF file and display on console in Java?
- How to loop through a menu list on a webpage using Selenium?
- How to loop through all the elements of an array in C#?
- How to identify elements based on text visible on page in Selenium?
- How can I list all cookies on the current page with JavaScript?
- How to download all images on a web page at once?
- Check whether Enter key is pressed or not and display the result in console with JavaScript?
- How to display all constraints on a table in MySQL?

Advertisements