+ 62

How to find any word in Textarea by JS ? [ Solved! ]

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 "); https://code.sololearn.com/WyyD61sS4GvH/?ref=app

13th Aug 2020, 7:40 AM
Prachi The Pari โญ
Prachi The Pari โญ - avatar
18 Answers
+ 17
di aap string handling search kijye google pe vaha aapko mil jayga ki how to handle strings in Js https://code.sololearn.com/WtCWNfqm2iUT/?ref=app Di analize it
13th Aug 2020, 7:43 AM
Abhiyantฤ
Abhiyantฤ - avatar
+ 28
Thank you so much ๐Ÿค— UmV6YQ== & Bugs & Namit Jain & Darshan Raval or Akash Kumar Srivastava Here are 4 answer helped me
13th Aug 2020, 8:02 AM
Prachi The Pari โญ
Prachi The Pari โญ - avatar
+ 19
13th Aug 2020, 8:03 AM
Reza
+ 19
Geekyorion Thank you so much it's also helping ๐Ÿค—
13th Aug 2020, 10:41 AM
Prachi The Pari โญ
Prachi The Pari โญ - avatar
+ 19
iamSamaritan Wow that's great idea Thank you so much ๐Ÿ˜€๐Ÿ‘
14th Aug 2020, 1:53 AM
Prachi The Pari โญ
Prachi The Pari โญ - avatar
13th Aug 2020, 7:59 AM
Reza
+ 17
Use includes method if (textarea.value.includes(word)) { alert("found"); } else { alert("not found"); }
13th Aug 2020, 7:55 AM
Reza
+ 10
First target your id var str = document.getElementbyId('idname').value Now you can use the search function / method var sch = str.search(any_word) If (sch != -1 ) alert( "word found" ) else alert("Not found") Hope this helps !!
13th Aug 2020, 8:00 AM
Bugs
Bugs - avatar
+ 8
Prachi The Pari๐Ÿ’• Hehe ๐Ÿ‘ฆ๐Ÿ’ƒ๐Ÿ’ƒ
13th Aug 2020, 8:44 AM
Namit Jain
Namit Jain - avatar
+ 7
๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡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") } }
13th Aug 2020, 8:24 AM
sky9262
sky9262 - avatar
13th Aug 2020, 10:34 AM
Geekyorion
+ 6
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" https://code.sololearn.com/WxIHZrMD0q5x/?ref=app
13th Aug 2020, 7:50 AM
Namit Jain
Namit Jain - avatar
+ 6
Anytime
13th Aug 2020, 10:46 AM
Geekyorion
+ 4
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
13th Aug 2020, 9:53 AM
Geekyorion
+ 4
https://code.sololearn.com/W3eHB44kiafX/?ref=app 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..
14th Aug 2020, 12:30 AM
imsamaritan
imsamaritan - avatar
+ 2
Mamรก culo
13th Aug 2020, 7:48 PM
Gustavo Bravo
Gustavo Bravo - avatar
+ 2
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.
13th Aug 2020, 11:51 PM
Togo Hamidou
Togo Hamidou - avatar
+ 2
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.
14th Aug 2020, 12:46 PM
BeastyDSK
BeastyDSK - avatar