How to find any word in Textarea by JS ? | SoloLearn: Learn to code for FREE!


How to find any word in Textarea by JS ?

Can someone plz help me how to find any word by JS ? Example : Textarea : I Love JavaScript & It's Very Intresting Language User Input : JavaScript or (Any word) Output : if word found then alert Alert ("Found Word "); else; Alert ("Not Found Word ");

8/13/2020 7:40:00 AM

Prachi The Pari🎸 [ Inactive ]

18 Answers

New Answer


di aap string handling search kijye google pe vaha aapko mil jayga ki how to handle strings in Js Di analize it


Thank you so much 🤗 UmV6YQ== & Bugs & Namit Jain & Darshan Raval or Akash Kumar Srivastava Here are 4 answer helped me


Use includes method if (textarea.value.includes(word)) { alert("found"); } else { alert("not found"); }


Prachi The Pari💕 I complete the function


Prachi The Pari💕 ur welcome :)


Geekyorion Thank you so much it's also helping 🤗


iamSamaritan Wow that's great idea Thank you so much 😀👍


First target your id var str = document.getElementbyId('idname').value Now you can use the search function / method var sch = If (sch != -1 ) alert( "word found" ) else alert("Not found") Hope this helps !!


To find a word you can use the syntax "indexOf" it is -1 when the word is not there in the text Try to type "namit jain"


Prachi The Pari💕 Hehe 👦💃💃


👇👇👇Try this 👇👇👇 function search(){ var str = 'My name is akash kumar srivastava'; var search = prompt("Enter your string to search ","akash"); var pos = str.indexOf(search); if(pos>0){ alert ("Found") } else { alert ("Not found") } }





Why don't you use regex match and can do this in a very less line of code. if you want, I can share the code

+3 Add items and start searching below.. Not that you'll do first search and get a message saying "match found" then maybe add more items and when you search / repeat the first search ... Wrong message will be displayed "scope confusion" But code is correct as found item / items will remain displayed while not found will disappear.. 👌.. You use search method but in that example indexOf() had be used..


Mamá culo


indexOf is the easiest way, but might not be the fastest way. Why isn't indexOf working for you? You will get the index of the match, and you know the length of the match, so just highlight the text that matched.


u can use var textarea = document.queryselector("textarea").textContent; if textarea.includes(userinput) alert(msg); else somthing explanation:- 1 --> in first line we have created a variable and stored the text from the textarea inside that variable. 2 --> next we have used a JS inbuilt string method ( string.includes(parameter) ) that will find the word we r providing it as a parameter. 3 --> if the value present in the text it will be true else false.