Home > Software design >  How do you check if user typed the specific word inside a textarea?
How do you check if user typed the specific word inside a textarea?

Time:03-14

How do you check if user typed the specific text inside the textarea tag? Here is an example

 </head>
 <body>
            
            <textarea>
                            
            </textarea>
            <div >
                            
            </div>
            <script>
                            (this is just example)
                            
                            if = user typed inside textarea this =("display (hi)")
                            do = build element inside .box the user typed inside the () of the text "display" 
            </script>

I really don't know how to do it i try to search it but none appeared and if you're confused with code it just glitched

CodePudding user response:

With the JS string function includesyou can archive this.

let block = false;
const box = document.querySelector('.box');

  function check(e) {
    const match = e.value.includes("hi");
    if (match && block === false) {
    box.innerHTML = 'Hello again'
    block = true;
  }  
}
<textarea onkeyup="check(this)"></textarea>
<div ></div>

CodePudding user response:

I hope it will give you idea. If use type any text and includes hi in textarea. It's will show all text in box container.

let textarea = document.querySelector("textarea");
let box = document.querySelector('.box');

textarea.addEventListener('keyup', (e) => {
  if (e.target.value.includes("hi")){
    box.innerHTML = "";
    box.append(`${e.target.value}`);
  }
})
<textarea></textarea>
<div ></div>

  • Related