JavaScript: How to Iterate/Loop HTML Elements

By Xah Lee. Date: . Last updated: .

The DOM get elements methods return either HTMLCollection and NodeList

How do you iterate them?

After JS2015

Since JS2015, both HTMLCollection and NodeList are Iterable Object.

Use for-of Loop.

or any Array Method with Spread Operator e.g. arrayMethod(...nodes)

Before JS2015

For before JS2015 code, HTMLCollection and NodeList are not iterable, but still are Array-Like Object. see Apply Array Method to Array-Like Object

Basic DOM Element Methods

BUY
Ξ£JS
JavaScript in Depth

DOM Scripting

Overview

DOM Methods

Basic Examples

HTML Input

Web Scripting Examples

Web Scripting Misc