HTML onsearch Event Attribute

The HTML onsearch event attribute is triggered when a user hit “Enter” key in an HTML input element with type=”text” in an HTML document.


Following is the syntax −

<input type=”search” onsearch=”script”>

Let us see an example of HTML onsearch event Attribute −


 Live Demo

<!DOCTYPE html>
   body {
      color: #000;
      height: 100vh;
      background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat;
      text-align: center;
      padding: 20px;
   input {
      border: 2px solid #fff;
      background: transparent;
      height: 2rem;
      padding: 10px;
      width: 200px;
   ::placeholder {
      color: #000;
   .show {
      font-size: 1.5rem;
      font-weight: bold;
<h1>HTML onsearch Event Attribute Demo</h1>
<input type="search" onsearch="searchFn()" placeholder="Search here">
<p>Enter what you want to search and then hit "Enter" key</p>
<div class="show"></div>
   function searchFn() { = "linear-gradient(45deg, #8BC6EC 0%, #9599E2 100%) no-repeat";
      document.querySelector(".show").innerHTML = "You search: " + document.querySelector("input").value;


Enter your search text in the search field and then hit “Enter” key to observe how onsearch event attribute works.

Updated on: 26-Sep-2019


Kickstart Your Career

Get certified by completing the course

Get Started