What is the usage of onsearch event in JavaScript?


The onsearch event is useful for search i.e. a user press ENTER or “x” key in input element. The type for <input> is search, since it is for users to search. The onsearch event isn’t supported in Internet Explorer, Firefox, and Opera.

Example

You can try to run the following code to learn how to implement onsearch event in JavaScript.

<!DOCTYPE html>
<html>
   <body>
      <p>Write what you want to search below and press "ENTER".</p>

      <input type = "search" id = "newInput" onsearch = "searchFunc()">
      <script>
         function searchFunc() {
            var a = document.getElementById("newInput");
            document.write("Searched = " + a.value);
         }
      </script>
   </body>
</html>

Updated on: 22-May-2020

247 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements