Home

SiteMap

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.

I have a texbox and textarea. See below. Enter a word in the "first" textbox that you want to check. Next, start typing in the "second" box. If it finds a match for the word in the first box, it will show a message.

The script.

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

    <p id="result"></p>
</body>
<script>
    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';
    }
</script>
Try it

➡️ .includes() method

includes() method examples:

1) check if a particular word is inside a <div> element

2) check if a certain element or value is inside an array

← PreviousNext →