Skip to content
Discussion options

You must be logged in to vote

This error usually happens because document.getElementById("username") is returning null, and null.value throws TypeError: Cannot read properties of undefined
Fix: Move your <script> tag to the end of the , or wrap your code in a DOMContentLoaded listener:

document.addEventListener("DOMContentLoaded", () => {
let userInput = document.getElementById("username").value;
console.log(userInput);
});

The ID doesn’t match
Double-check that your input has id="username" exactly (case-sensitive, no typos).

Code runs before user interacts
If you want the latest value, you might want to read it inside an event (like a button click).
document.getElementById("submitBtn").addEventListener("click", () => {

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@suswhopper
Comment options

Answer selected by suswhopper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Ask and answer questions about GitHub features and usage Programming Help Discussions around programming languages, open source and software development
2 participants