HTML onsearch Attribute

HTML onsearch Attribute: The HTML onsearch attribute can be defined as it fires when a user presses the “ENTER” key or clicks the “x” button in an <input> element with type=”search”. The onsearch attribute is the part of the Event Attributes.

HTML onsearch Attribute

This HTML onsearch attribute can be applied on the <input type=”search”> element.

Syntax: <element onsearch=”script”>

Browser Support

The HTML onsearch attribute is supported by the following browsers:

  • Chrome
  • Firefox
  • Internet Explorer
  • Safari
  • Opera

Example: for executing a JavaScript when submitting a search

<!DOCTYPE html>
<html>
<body>
<p>Write something in the search field and press "ENTER".</p>
<input type="search" id="myInput" onsearch="myFunction()">
<p id="demo"></p>
<script>
function myFunction() {
   var x = document.getElementById("myInput");
   document.getElementById("demo").innerHTML = "You are searching for: " + x.value;
}
</script>

</body>
</html>

Output:

HTML onsearch attribute