How to loop through HTML elements without using forEach() loop in JavaScript ?
Last Updated :
18 Jun, 2024
In this article, we will learn how to loop through HTML elements without using the forEach() method.
This can be done in the following ways:
Approach 1: Using the for loop
The HTML elements can be iterated by using the regular JavaScript for loop. The number of elements to be iterated can be found using the length property. The for loop has three parts, initialization, condition expression, and increment/decrement expression. Each of the items can be accessed by using square brackets with their respective index.
Syntax:
for (let i = 0; i < elements.length; i++) {
console.log(elements[i]);
}
 Example: In this example, we will see the use of the for loop.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>
How to loop through HTML elements
without using forEach() loop
in JavaScript ?
</title>
</head>
<body>
<!-- HTML elements to iterate -->
<p>This is paragraph 1.</p>
<p>This is paragraph 2.</p>
<p>This is paragraph 3.</p>
<script type="text/javascript">
// Get the elements to be iterated
let htmlElements =
document.getElementsByTagName("p");
// Use a regular for-loop
for (let i = 0; i < htmlElements.length; i++) {
// Print the current element
console.log(htmlElements[i]);
}
</script>
</body>
</html>
Output:

Output on the browser
Approach 2: Using the While loop
The HTML elements can be iterated by using a regular JavaScript while loop. The number of elements can be found using the length property. A temporary value is used to keep track of the current iteration by checking it in the condition expression. Each of the items can then be accessed by using square brackets with their respective index.
Syntax:
let i = 0;
while(i < elements.length) {
console.log(elements[i]);
i++;
}
Example: In this example, we will see the use of Using the While loop.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>
How to loop through HTML
elements without using
forEach() loop in JavaScript ?
</title>
</head>
<body>
<!-- HTML elements to iterate -->
<p>This is paragraph 1.</p>
<p>This is paragraph 2.</p>
<p>This is paragraph 3.</p>
<script type="text/javascript">
// Get the elements to be iterated
let htmlElements =
document.getElementsByTagName("p");
// Define a variable to keep track
// of the current iteration
let i = 0;
// Check if the current value
// is lesser than total elements
while (i < htmlElements.length) {
// Print the current element
console.log(htmlElements[i]);
// Increment the value
i++;
}
</script>
</body>
</html>
Output:

Output
The for…of statement can be used to loop over values of an iterable object. It includes objects like an Array, Map, Set, or HTML elements. A temporary variable holds the current value during the execution of the loop, which can then be used in the body of the loop.
Syntax:
for (element of elements) {
console.log(element);
}
Example: In this example, we will see the use of Using the ‘for…..of’ statement.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>
How to loop through HTML
elements without using
forEach() loop in JavaScript ?
</title>
</head>
<body>
<!-- HTML elements to iterate -->
<p>This is paragraph 1.</p>
<p>This is paragraph 2.</p>
<p>This is paragraph 3.</p>
<script type="text/javascript">
// Get the elements to be iterated
let HTMLelements =
document.getElementsByTagName("p");
// Use the for...of statement to get
// the values
for (elem of HTMLelements) {
// Print the current element
console.log(elem);
}
</script>
</body>
</html>
Output:

Output
Approach 4: Using the for…in statement
The for…in statement can be used to loop over the enumerable properties of an object or the indices of an array-like object. While this is not the most common use case for for…in, it can be applied to HTMLCollection to iterate over its indices.
Syntax:
for (let index in elements) {
if (elements.hasOwnProperty(index)) {
console.log(elements[index]);
}
}
Example: In this example, we will see the use of the for…in statement.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>How to loop through HTML elements without using forEach() loop in JavaScript?</title>
</head>
<body>
<!-- Nikunj Sonigara -->
<!-- HTML elements to iterate -->
<p>This is paragraph 1.</p>
<p>This is paragraph 2.</p>
<p>This is paragraph 3.</p>
<script type="text/javascript">
// Get the elements to be iterated
let htmlElements = document.getElementsByTagName("p");
// Use the for...in statement to get the indices
for (let index in htmlElements) {
if (htmlElements.hasOwnProperty(index)) {
// Print the current element
console.log(htmlElements[index]);
}
}
</script>
</body>
</html>
Similar Reads
How to get the text without HTML element using JavaScript ?
Given an HTML document containing some elements and the task is to get the text inside an HTML element using JavaScript. There are two methods to get the text without HTML element which are listed below: Using innerText propertyUsing textContent property Using innerText property: We can use innerTex
1 min read
How to find the position of HTML elements in JavaScript ?
In this article, we are given an HTML document and the task is to get the position of any element by using JavaScript. Use the following steps to get the position: Step 1: Selecting an element: Before finding the position, it is necessary to select the required HTML element. Every element in HTML is
3 min read
How to use async/await with forEach loop in JavaScript ?
Asynchronous is popular nowadays because it gives functionality of allowing multiple tasks to be executed at the same time (simultaneously) which helps to increase the productivity and efficiency of code. Async/await is used to write asynchronous code. In JavaScript, we use the looping technique to
2 min read
How to add an element horizontally in Html page using JavaScript?
There can be some cases where we have the requirement to add elements in a horizontal manner. Like, if your elements are linked list nodes and you want to add them horizontally. Now the question arises how we can do this in better way.One approach can be to use âdisplayâ property with the value âinl
3 min read
How to Loop Through an Array in JavaScript?
Here are the various ways to loop through an array in JavaScript 1. Using for LoopThe for loop is one of the most used ways to iterate over an array. It gives you complete control over the loop, including access to the array index, which can be useful when you need to modify elements or perform othe
4 min read
How to get/change the HTML with DOM element in JavaScript ?
In order to get/access the HTML for a DOM element in JavaScript, the first step is to identify the element base on its id, name, or its tag name. Then, we can use inner.HTML or outer.HTML to get the HTML. Using the getElementById() method: This method gets/identifies the DOM elements using its ID an
3 min read
How to map array values without using map method in JavaScript ?
Array elements can be mapped by using looping methods in JavaScript. The map() method creates a new array with the results of the output of a function called for each array element. This can also be implemented using a for loop in JavaScript. Approach 1: For this, we can create two arrays, in which
3 min read
How to search an element without using any loops in Node.js ?
The setInterval() method repeats or re-schedules the given function at every given time-interval. It is somewhat like window.setInterval() Method of JavaScript API, however, a string of code canât be passed to get it executed. Syntax: setInterval(timerFunction, millisecondsTime); Parameter: It accep
3 min read
How to replace an HTML element with another one using JavaScript?
Replacing an HTML element with another using JavaScript allows for dynamic modification of webpage content without reloading the entire page. Document Object Model (DOM) is a platform and language-neutral interface that is used by programs and scripts to dynamically access the content, style, and st
2 min read
How to remove an HTML element using JavaScript ?
Removing an HTML element using JavaScript involves deleting it from the DOM, typically using methods like element.remove() or parentNode.removeChild(). This approach updates the webpage dynamically, allowing for responsive interactions by removing unwanted or outdated elements based on user actions
3 min read