<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>getElementsByClassName()</title>
    </head>
    <body>
        <ul>
            <li class="a">Hamburger</li>
            <li class="a">Hot Dog</li>
            <li class="a">French Fries</li>
        </ul>
        <script src="class-name.js"></script>
    </body>
</html>
const liElements = document.getElementsByClassName("a");
console.log(liElements); // HTMLCollection(3) [li.a, li.a, li.a]

/* Approach 1: Using for...of */
for (const liElement of liElements) {
    console.log(liElement.textContent);
}
// Output: "Hamburger" "Hot Dog" "French Fries"

/* Approach 2: Using a standard for loop (The length property is accessible) */
for (let i = 0; i < liElements.length; i++) {
    console.log(liElements[i].textContent);
}
// Output: "Hamburger" "Hot Dog" "French Fries"

/* Approach 3: Converting to an array using the spread operator (...) */
const arr = [...liElements];
arr.forEach(li => {
    console.log(li.textContent);
});
// Output: "Hamburger" "Hot Dog" "French Fries"

/* Approach 4: Converting to an array using Array.from() */
const liElementsArray = Array.from(liElements);
liElementsArray.forEach(li => {
    console.log(li.textContent);
});
// Output: "Hamburger" "Hot Dog" "French Fries"
// In 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>getElementsByClassName() - Finding Elements with Multiple Class Names</title>
    </head>
    <body>
        <ul>
            <li class="a">Hamburger</li>
            <li class="a b c">Hot Dog</li>
            <li class="a b">French Fries</li>
        </ul>
        <script src="test-1.js"></script>
    </body>
</html>
/* Find elements that have both class names a and b */
const liElements = document.getElementsByClassName("a b");

for (const liElement of liElements) {
    console.log(liElement.textContent);
}
// Output: "Hot Dog" "French Fries"
getElementsByClassName(name)[n]
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>getElementsByClassName(name)[n]</title>
    </head>
    <body>
        <ul>
            <li class="a">Hamburger</li>
            <li class="a">Hot Dog</li>
            <li class="a">French Fries</li>
        </ul>
        <script>
            const liElements = document.getElementsByClassName("a");

            /* The first element with the class name a in the HTML document */
            console.log(liElements[0].textContent); // Index starts at 0
            // Output: "Hamburger"
        </script>
    </body>
</html>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>getElementsByClassName().length</title>
    </head>
    <body>
        <ul>
            <li class="a">Hamburger</li>
            <li class="a">Hot Dog</li>
            <li class="a">French Fries</li>
        </ul>
        <script>
            const liElements = document.getElementsByClassName("a");
            console.log(liElements.length); // Output: 3
        </script>
    </body>
</html>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>element.getElementsByClassName()</title>
    </head>
    <body>
        <h1>element.getElementsByClassName()</h1>
        <h2>Diner Menu</h2>
        <ul>
            <li>Hamburger</li>
            <li>Hot Dog</li>
            <li>French Fries</li>
        </ul>
        <h2>Chinese Restaurant Menu</h2>
        <div id="chinese-menu">
            <ul>
                <li class="a">Orange Chicken</li>
                <li class="a">Lo Mein</li>
                <li class="a">Spring Rolls</li>
            </ul>
        </div>
        <script>
            const chineseMenu = document.getElementById("chinese-menu");

            // Find all elements with the class name "a" within the element with the id "chinese-menu"
            const chineseMenuLiElements = chineseMenu.getElementsByClassName("a");

            // Output the text content of the first element among the found elements
            console.log(chineseMenuLiElements[0].textContent); // Output: "Orange Chicken"
        </script>
    </body>
</html>