// Across the entire HTML document
document.getElementsByTagName(name)

// Within a specific element
element.getElementsByTagName(name)
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>HTMLCollection</title>
    </head>
    <body>
        <ul>
            <li>Pizza</li>
            <li>Burger</li>
            <li>Pasta</li>
        </ul>
        <script src="tag-name.js"></script>
    </body>
</html>
const liElements = document.getElementsByTagName("li"); // Select all <li> elements
console.log(liElements); // HTMLCollection(3) [li, li, li]

/* Method 1 - Using for...of loop */
for (const liElement of liElements) {
    console.log(liElement.textContent);
}
// Output: "Pizza" "Burger" "Pasta"

/* Method 2 - Using traditional for loop */
for (let i = 0; i < liElements.length; i++) { // length property available
    console.log(liElements[i].textContent);
}
// Output: "Pizza" "Burger" "Pasta"

/* Method 3 - Convert to an array and use the array's forEach() method */

// Convert to array using spread syntax
const arr = [...liElements];

arr.forEach(li => {
    console.log(li.textContent);
});
// Output: "Pizza" "Burger" "Pasta"

// Convert to array using Array.from()
const liElementsArray = Array.from(liElements);

liElementsArray.forEach(li => {
    console.log(li.textContent);
});
// Output: "Pizza" "Burger" "Pasta"
Across the entire HTML document
document.getElementsByClassName(names)

// Within a specific element
element.getElementsByClassName(names)
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>HTMLCollection</title>
    </head>
    <body>
        <ul>
            <li class="menu-item">Pizza</li>
            <li class="menu-item">Burger</li>
            <li class="menu-item">Pasta</li>
        </ul>
        <script src="class-name.js"></script>
    </body>
</html>
const liElements = document.getElementsByClassName("menu-item");
console.log(liElements); // HTMLCollection(3) [li.menu-item, li.menu-item, li.menu-item]

/* Method 1 - Using for...of loop */
for (const liElement of liElements) {
    console.log(liElement.textContent);
}
// Output: "Pizza" "Burger" "Pasta"

/* Method 2 - Using traditional for loop */
for (let i = 0; i < liElements.length; i++) {
    console.log(liElements[i].textContent);
}
// Output: "Pizza" "Burger" "Pasta"

/* Method 3 - Converting to an array and using the array’s forEach() method */

// Convert to array using spread syntax
const arr = [...liElements];

arr.forEach(li => {
    console.log(li.textContent);
});
// Output: "Pizza" "Burger" "Pasta"

// Convert to array using Array.from()
const liElementsArray = Array.from(liElements);

liElementsArray.forEach(li => {
    console.log(li.textContent);
});
// Output: "Pizza" "Burger" "Pasta"
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>HTMLCollection Live Example</title>
</head>
<body>
    <ul id="list">
        <li>Seaweed Roll</li>
        <li>Ramen</li>
        <li>Spicy Rice Cake</li>
    </ul>
    <p>Number of <code><li></code> elements: <strong id="li-length-val">3</strong></p>
    <button type="button" id="addButton">Add New Menu Item</button>
    <script src="live.js"></script>
</body>
</html>
// Get the HTMLCollection of <li> elements
const liElements = document.getElementsByTagName("li");

// Add event listener to the button to add a new <li> element
document.getElementById("addButton").addEventListener("click", () => {
    const newLi = document.createElement("li");
    newLi.textContent = "New Menu Item";

    // Append the new <li> element to the <ul> with id "list"
    document.getElementById("list").appendChild(newLi);

    // Update the displayed count of <li> elements in real-time
    const liElementsLength = liElements.length; // Length reflects live updates
    document.getElementById("li-length-val").textContent = liElementsLength;
});
Live Demo Click the demo button below!