How to check if textarea contains a particular word using JavaScript

← PrevNext →

Let us assume, I have a textarea element on my web page and I want to check if the textarea has a particular word or string typed in it. There are many ways you can do this. But, here I'll show you a simple method to look for certain word in a textarea using JavaScript.

Below is a textarea. Now, when you type something in the textarea, a JS script would check if the particular word, which you want to check, is typed in it.

Enter the word that you want to check in the first box.

Here's the script.

    <p>Word to check... <input type="text" value="the" id="tb" /></p>
        <textarea onkeyup='findWord(this)' id='ta' rows="3" cols="40" 
           placeholder="start typing here..."></textarea>

    <p id="result"></p>
    let findWord = (el) => {
        let wrd_2_find = document.getElementById('tb').value.toLowerCase();
        let ta = document.getElementById(el.id).value.toLowerCase();
        document.getElementById('result').innerHTML = 
            ta.includes(wrd_2_find) == true ? 'word exits' : 'no match';
Try it

You can use the includes() method in many other ways, like check if a particular word is inside a <div> element or use the method to check if a certain element or value is inside an array.

Happy coding. 😀

← PreviousNext →