
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to correctly iterate through getElementsByClassName() in JavaScript?
To correctly iterate, use the document.getElementsByClassName(). The getElementsByClassName() is a methof od Document interface.
Following is the cod wherein we are iterating through div classes −
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> <div class="demo">My First JavaScript Program</div> <div class="demo">This is the second Program</div> <div class="demo">This is the third Program </div> <script> var className = document.getElementsByClassName('demo'); for(var index=0;index < className.length;index++){ console.log(className[index].innerHTML); } </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 iterate through Java List?
- How to iterate through a dictionary in Python?
- How to iterate through a list in Python?
- How to iterate through a tuple in Python?
- Iterate through ArrayList in Java
- Iterate through Object keys and manipulate the key values in JavaScript
- Iterate through Decade Tuple in Java
- Iterate through Ennead Tuple in Java
- Iterate through Octet Tuple in Java
- Iterate through LabelValue Tuple in Java
- Iterate through KeyValue Tuple in Java
- Iterate through Triplet class in JavaTuples
- Iterate through Septet class in JavaTuples
- Iterate through Quartet class in JavaTuples
- How do I iterate through DOM elements in PHP?

Advertisements