Jump to content

Javascript code doesn't run


Beginner2701

Recommended Posts

Hello together,

I am learning JavaScript at the moment and I cannot run a simple code. I do not know if there is a mistake or if I have to install something first!? Quite frustrating since I am at the very beginning.

My code is shown below. It is more or less exactly the code from a lesson I was recently doing, so it should be correct. I just wanted to do a first test to update a webpage by pressing a button. The page should present the username + "signed up" after entering the name and pressing the button. However, if I test it in my firefox it doesn't work. Does also not work in Edge. Is there a mistake in the code? I saved the file under test.html in case that's relevant.

I would appreciate your help!

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>test</title>
  </head>
  <body>

<input id="usernameInput" type="text" placeholder="username">
<button onclick="signUp()">Sign up</button>
<p id="message"></p>

<script type="text/javascript">

function signUp() {
  var username = document.getElementsByID("usernameInput").value;
  document.getElementsByID("message").innerHTML = username + " signed up";
}

</script>

  </body>
</html>

 

Link to comment
Share on other sites

You should check the browser's Javascript console for errors. Most browsers let you see the console by pressing F12 on your keyboard. This will help you fix problems in the future.

Right now, the error console would tell you that getElementsByID does not exist. The name of the method is getElementById. The "d" is lowercase and "Element" is not plural.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...