Reputation: 114
When I type in an ID number for the game, I always gets the 3rd statement of my if else expression. Where am I going wrong?!!
<html>
<body>
<p>Type in the ID and hit search:</p>
<button onclick="myFunction()">Search</button>
<p id="demo"></p>
<form>
<input id="textbox" type="text" />
</form>
<script>
var textboxValue = document.getElementById("textbox").value;
function myFunction() {
var message;
if (textboxValue == 1) {
message = "Fantasy World";
} else if (textboxValue == 2) {
message = "Sir Wags A Lot";
} else {
message = "Take a Path";
}
document.getElementById("demo").innerHTML = message;
}
</script>
</body>
</html>
Upvotes: 0
Views: 71
Reputation: 382102
The problem is you always test the same, initial, value of the input.
Change
var textboxValue = document.getElementById("textbox").value;
function myFunction() {
to
function myFunction() {
var textboxValue = document.getElementById("textbox").value;
This way the value will be read each time the function is called.
Upvotes: 8
Reputation: 376
Put this statement:
var textboxValue = document.getElementById("textbox").value;
Inside the function.
Upvotes: 4