<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta charset="utf-8">
        <title>getElementById()の例</title>
    </head>
    <body>
        <p id="hello">ようこそ。お会いできてうれしいです!</p>
        <script>
            const changeColor = newColor => {
                const ele = document.getElementById("hello"); // id="hello"と一致する要素を取得します。
                ele.style.color = newColor;
            }
        </script>
        <button type="button" onclick="changeColor('red');">赤色に変える</button>
        <button type="button" onclick="changeColor('blue');">青色に変える</button>
    </body>
</html>
実際の適用例 ボタンをクリックしてみてください!
document.getElementById(id);
<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta charset="utf-8">
        <title>探している要素がない場合にエラーが発生する例</title>
    </head>
    <body>
        <button type="button" id="btn1">btn1ボタン</button>
        <button type="button" id="btn2">btn2ボタン</button>
        <button type="button" id="btn3">btn3ボタン</button>
        <script>
            /* id="btn-4"の要素はありません。 */
            const eleBtn4 = document.getElementById("btn-4"); // id="btn-4"と一致する要素を取得します。
            
            // eleBtn4は要素オブジェクトではなくnullになります。

            // Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')
            eleBtn4.addEventListener("click", () => {
                // ...
            });
        </script>
    </body>
</html>
<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta charset="utf-8">
        <title>探している要素がない場合にエラーが発生する例</title>
    </head>
    <body>
        <button type="button" id="btn1">btn1ボタン</button>
        <button type="button" id="btn2">btn2ボタン</button>
        <button type="button" id="btn3">btn3ボタン</button>
        <script>
            /* id="btn-4"の要素はありません。 */
            const eleBtn4 = document.getElementById("btn-4"); // id="btn-4"と一致する要素を取得します。
            
            // eleBtn4は要素オブジェクトではなくnullになります。

            if (eleBtn4) {
                eleBtn4.addEventListener("click", () => {
                    // ...
                });
            } else {
                console.warn("idが「btn-4」の要素を取得できません。");
            }
        </script>
    </body>
</html>
<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta charset="utf-8">
        <title>探している要素がない場合にエラーが発生する例</title>
    </head>
    <body>
        <button type="button" id="btn1">btn1ボタン</button>
        <button type="button" id="btn2">btn2ボタン</button>
        <button type="button" id="btn3">btn3ボタン</button>
        <script>
            /* id="btn-4"の要素はありません。 */
            const btn4 = document.getElementById("btn-4"); // id="btn-4"と一致する要素を取得します。

            // btn4は要素オブジェクトではなくnullになります。

            try {
                btn4.addEventListener("click", () => {
                    // ...
                });
            } catch (error) {
                console.warn("idが「btn-4」の要素を取得できません。");
            }
        </script>
    </body>
</html>
<div id="user-id"></div>
getElementById("User-id");
getElementById("User-Id");
getElementById("USER-ID");
getElementById("user-id");
<div id="container">
    <div id="child"></div>
</div>
const container = document.getElementById("container");
// idが一致する要素を探してElementオブジェクトを返します。
// 「container」は「documentオブジェクト」ではなく「Elementオブジェクト」です。

// Uncaught TypeError: container.getElementById is not a function
const child = container.getElementById("child");
const child = document.getElementById("child");