0

I'm new to javascript. I'm trying to make a laser tag game to share with my friends. However, I want it to be like Kahoot It where I can enter a game PIN to take you to the right game. However, I do not know how to tell my javascript which game I want to be taken to. Here is my code:

body {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  background-color: black;
}

.logo {
  width: 50%;
  display: block;
  margin-left: auto;
  margin-right: auto;
  margin-top: 7%;
}

.codeinput {
  width: 20%;
  size: 30%;
  display: block;
  margin-left: auto;
  margin-right: auto;
}

.codeinput:focus {
  outline: none;
}

.enterbutton {
  border: none;
  background-color: #018D94;
  padding: 1% 4%;
  border-radius: 5%;
  display: block;
  margin-left: auto;
  margin-right: auto;
  margin-top: 2%;
}

.enterbutton:hover {
  background-color: #036266;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Laser Tag</title>
  <link rel="stylesheet" href="css/index.css">
</head>

<body>
  <img src="img/logo.png" class="logo" alt="Laser Tag">
  <input type="text" name="" value="" placeholder="Game PIN" class="codeinput" id="codeinput">
  <button type="button" name="button" class="enterbutton" onclick="enterfunc">ENTER</button>
</body>
<script src="js/index.js" charset="utf-8"></script>
<script type="text/javascript">
  var codeinput = document.getElementById('codeinput').value;

  function enterfunc() {
    if (codeinput == '2147') {
      alert('Great! This works!');
    } else {
      alert('Hmm...something in your code is not right');
    }

  }
</script>

</html>

9
  • 1
    var codeinput = document.getElementById('codeinput').value; is read on page load. It will not magically update the variable.... Move it inside of the function..... Commented Sep 17, 2020 at 13:22
  • var codeinput = document.getElementById('codeinput').value; – that would belong into your function here, so that the current value gets read, when the button gets clicked. Otherwise, you are only reading the value the field had at the time this line of code executed (and that was just empty at this time.) Commented Sep 17, 2020 at 13:22
  • 1
    also use onclick="enterfunc()" not just func name so that JS can execute that function code. Commented Sep 17, 2020 at 13:25
  • if(document.getElementById('codeinput').value =='2147') will give your desired output Commented Sep 17, 2020 at 13:25
  • 1
    You can also use var codeinput = document.getElementById('codeinput'); to store the element itself, then use if (codeinput.value == '2147') to read the current value inside the function. Commented Sep 17, 2020 at 13:26

1 Answer 1

1

You just have to move var codeinput = document.getElementById('codeinput').value; into your function like this:

function enterfunc() {

  var codeinput = document.getElementById('codeinput').value;

  if (codeinput == '2147') {
    alert('Great! This works!');
  } else {
    alert('Hmm...something in your code is not right');
  }

}

This way, always the current input is read.

Also you have to call the function correctly in your HTML:

<button type="button" name="button" class="enterbutton" onclick="enterfunc()">ENTER</button>

Since enterfunc() is a function, you have to use the brackets inside the button tag.

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

6 Comments

onclick="enterfunc"
hi! but, when i put in the number 2147, i get the error message saying that something is not right. could you help me please?
@SiannaZewdie maybe you can try if (codeinput == 2147) without quotes?
Can you send a link? Because the JSFiddle above doesn't work...
It does work! You still did not move your variable declaration inside the function!! Look at my code. var codeinput = document.getElementById('codeinput').value; has to be inside enterfunc().
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.