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>
1 Respuesta
+ 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.