Jump to content

Javascript code doesn't run


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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...