Why does it log out "null" on the console on JS?

Hello. This is my code:

function saveInput() {
    console.log("Button clicked from onclick attribute")
}

let inputBtn = document.getElementById("input-btn")
console.log(inputBtn)

And it logs out “null”. It’s part of a video tutorial by Per and I’ve copied his entire code, but I keep getting “null”. Please, help.
Thanks all!

You get null when getElementById doesn’t find any element. if you post all your code maybe we can help more

This is the rest:

<head>
    <link rel="stylesheet" href="index.css">
</head>
<body>
    <script src="index.js"></script>
    <input id="input-el" type="text">
    <button id="input-btn" onclick="saveInput()">SAVE INPUT</button>
</body>

Oh wow!!! Thank you so much man!!! Something so simple had me about to lose my mind. :sweat_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.