3

Hi I'm trying to make a log in program that authenticates usernames only so when I try to pass user input from html to javascript it picks it up as null does anyone know why? Gives me this error message: Login2.js:12

   Uncaught TypeError: Cannot read properties of null (reading 'value')
at HTMLButtonElement.document.getElementById.onclick (Login2.js:12:55)

HTML:

    <!DOCTYPE html>
<html>
<head>
    <title>StudySkillsApp Log-in</title>
</head>

<body>
<label><b>Username:</b></label>
<input placeholder="Enter Username" id={"myText"}>
<button type="button" id="myButton"> SIGN IN </button>
<script src="Login2.js">
</script>
</body>

</html>

And Login2.js(javascript is):

if(document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', afterLoaded);
} else {
    //The DOMContentLoaded event has already fired. Just run the code.
    afterLoaded();
}

function afterLoaded(){
    //Your initialization code goes here. This is from where your code should start
// document.readyState === "complete")
    document.getElementById("myButton").onclick = function(){
        var myName = document.getElementById("myText").value;
        console.log(myName);
    }
};
1
  • id={"myText"} is invalid syntax and must be id="myText". This invalid syntax results in the element not having that id, thus it cannot be found. Commented Mar 8, 2022 at 13:56

3 Answers 3

4

You need to remove {} brackets from your HTML code. On the input tag replace id={"myText"} with id="myText"

<input placeholder="Enter Username" id="myText">

It will fix the issue.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. Why does it do that? if you don't mind me asking(because I'm trying to improve my knowledge in javascript)
because when you use id={"myText"} on input tag, id will be set as {"myText"} not "myText". Everything inside "" will be considered as id. You can check it by inspecting it in chrome dev tools. if you use {"myText"} (in JS file )as id in getElementById, it will work fine again. eg. var myName = document.getElementById('{"myText"}').value;
0

I came across the same error while working on my project. If you are sure everything is right but still getting the error, most likely your external js file(s) trying to reach DOM elements before DOM is ready. You should use defer or async in your script tag.

It also works when adding the <script> tag at the end of the <body> (not recommended for long HTML documents, there may be a noticeable delay).

Comments

0

I was looking here to help fix a similar issue I was having only to find that my problem was a mismatch of single and double quotations. I don't know exactly where the issue came from but having

<canvas id="background" width="100%" height="100%"></canvas>

match

var gameCanvas = document.getElementById("gameCanvas");
var gameContext = gameCanvas.getContext("2d");

was all it took

2 Comments

I don't understand your answer, let alone how it addresses the question. Yes, the id "background" does not match the id "gameCanvas", but what does that have to do with single/double quotes?
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.