0

I'm trying to do a very simple thing, but I can't. I would like to be redirected to a specific page depending on the input value entered by the user on pressing the enter key. However I can't and every time I am redirected to the same page. What am I doing wrong ? this is the code

    window.onkeyup = keyup;


var inputTextValue;

function keyup(e) {
  //setting your input text to the global Javascript Variable for every key press
  inputTextValue = e.target.value;
  console.log(inputTextValue);
 
  if (e.keyCode == 13) {
    if (inputTextValue == "myInput") {
      return true;
      //   window.location = "http://www.google.it";
    } else {
      return false;
      //   window.location = "http://www.facebook.it";
    }
  }
}
  • Does this answer your question? [How do I redirect to another webpage?](https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage) – Henry Woody Jul 20 '20 at 16:34

1 Answers1

0

I used window.location.href property to change the url of page

document.querySelector("#btn").addEventListener("click", () => {
  const text = document.querySelector("#inputRedirectText").value;

  if (text === "myName") {
    window.location.href = "https://www.google.com";
  } else {
    window.location.href = "https://www.facebook.com";
  }
});
<input type="text" id="inputRedirectText" placeholder="type ..."/>
<button id="btn">Done</button>
Ferin Patel
  • 3,424
  • 2
  • 17
  • 49