bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/JavaScript/Objects, Classes, and Advanced Patterns
JavaScript•Objects, Classes, and Advanced Patterns

JavaScript HTML DOM Collections

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind JavaScript HTML DOM Collections?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ myCollection = document.getElementsByTagName("p");
3Order

Put the learning moves in the order that makes the concept easiest to apply.

The getElementsByTagName() method returns an HTMLCollection object.
HTML HTMLCollection Length
The HTMLCollection Object

The HTMLCollection Object

The getElementsByTagName() method returns an HTMLCollection object.

An HTMLCollection object is an array-like list (collection) of HTML elements.

The following code selects all <p> elements in a document:

Example

const myCollection = document.getElementsByTagName("p");

Note

The index starts at 0.

HTML HTMLCollection Length

The length property defines the number of elements in an HTMLCollection :

myCollection.length

The length property is useful when you want to loop through the elements in a collection:

Example

const myCollection = document.getElementsByTagName("p");
for (let i = 0; i < myCollection.length; i++) {
  myCollection[i].style.color = "red";
}

An HTMLCollection is NOT an array!

An HTMLCollection may look like an array, but it is not.

You can loop through the list and refer to the elements with a number (just like an array).

However, you cannot use array methods like valueOf(), pop(), push(), or join() on an HTMLCollection.

Previous

JavaScript HTML DOM Elements (Nodes)

Next

JavaScript HTML DOM Node Lists