I am trying to create a text adventure game. In it, the user types a command in a text input box. Based on the command, they will be sent to another web page. Here’s what I have for HTML:
<input type="text" id="a" onchange="text()" value=""/>
Here’s what I have for javascript:
JavaScript
x
11
11
1
function text(){
2
var input = document.getElementById("a").value;
3
4
switch(input){
5
case "run":
6
window.location.replace("1_2.html");
7
case "rescue":
8
window.location.replace("1_3.html");
9
}
10
}
11
But, if they type run or rescue, it sends them to 1_3.html. I have tried switching window.location.replace
with window.location.href
but they are not taken to 1_3.html nor 1_2.html. I have also tried using if else if else
but it gets the same results and problems. What should I do?
Advertisement
Answer
You can do it like
JavaScript
1
11
11
1
function text(){
2
var input = document.getElementById("a").value;
3
4
switch(input.toLowerCase()){
5
case "run":
6
window.location.replace("1_2.html");break;
7
case "rescue":
8
window.location.replace("1_3.html");break;
9
}
10
}
11
Because your input may contain upper and Lower case