Home > OS >  How to sense a particular letter in user input in JS
How to sense a particular letter in user input in JS

Time:10-17

If I type something with the letter 1 it should redirect to another page for eg: 1sometxt.
if not it should redirect to another page

function functionclick() {
     var input = document.getElementById("myInput").value;
     var input1 = "1";
     if (input === input1   i dont know what to give here ) {
         window.open("www.example.com");
     }else {
window.open("www.example.com");
}
 }

CodePudding user response:

document.getElementById("myInput").addEventListener("change"), (e)=>{

     var input = e.target.value
    
     if (input.includes(1) ) {
         window.open("www.example.com");
     }else {
         window.open("www.example.com");
     }
}

CodePudding user response:

You can check if input contain '1' by using input.includes('1'), or if '1' should be the first character the if condition could be something like this

if(input[0] === input1) { // doesn't matter what next characters are, check if input first character equal to input1 variable
  // your code here
}
  • Related