how can i access the text filled in input type search in js to use it in alert.please check below one what is wrong. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how can i access the text filled in input type search in js to use it in alert.please check below one what is wrong.

(inside html) <input type="search" id="sbar"> <button onclick="notfound()">search</button> <script> var searchtext = document.getElementById("sbar").innerText ; function notfound(){ alert( searchtext + " not fouund "); } </script>

29th May 2020, 2:45 AM
Divya Mohan
Divya Mohan - avatar
1 Answer
+ 3
To get the value of an input element we need to use the value property. var searchtext = document.getElementById("sbar").value; The value you got here is static and refers to the initial value of your input(an empty string). To get the correct result you need to retrieve the value when the input changes using an event handler, or simply move searchtext to your notfound function and get the value when user clicks the button.
29th May 2020, 4:37 AM
Kevin ★