1

I have a textarea in an html file. I am using the javascript code below. I want that when the user enters in the textareathe following sentences (with the break line),

hello
hi
how are you

javascript redirects him to a website. I tried to do this but didn't work. CAN ANYBODY PLEASE HELP ME? THANKS IN ADVANCE:)


Javascript code

function myFunction() {
var x = document.getElementById("myTextarea").value;
if (x === "hello" + \n + "hi" + \n + "how are you") {
  document.location.href = "http://www.google.com";
  return false;
 }
  else {
  alert('ok i knew it');
  return false;
 }
}
2
  • \ns should also be enclosed within quotes. Commented Jan 18, 2014 at 9:14
  • If you know what value to compare then put the value in the textarea, log its value in console and use this value for comparison in the if statement. Commented Jan 18, 2014 at 9:36

1 Answer 1

2

Escape sequence have to be inside quotes:

if (x === "hello" + "\n" + "hi" + "\n" + "how are you") {
    document.location.href = "http://www.google.com";
    return false;
}

or simply:

if (x === "hello\nhi\nhow are you") {
    document.location.href = "http://www.google.com";
    return false;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.