Skip to content
Advertisement

How to send users to a new web page based off of text input object

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:

function text(){
    var input = document.getElementById("a").value;

    switch(input){
        case "run":
            window.location.replace("1_2.html");
        case "rescue":
            window.location.replace("1_3.html");
    }
}

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

function text(){
    var input = document.getElementById("a").value;

    switch(input.toLowerCase()){
        case "run":
            window.location.replace("1_2.html");break;
        case "rescue":
            window.location.replace("1_3.html");break;
    }
}

Because your input may contain upper and Lower case

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement