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.

Syntax

Following is the syntax −

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

Let us see an example of HTML onsearch event Attribute −

Example

 Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
   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;
   }
</style>
</head>
<body>
<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>
<script>
   function searchFn() {
      document.body.style.background = "linear-gradient(45deg, #8BC6EC 0%, #9599E2 100%) no-repeat";
      document.querySelector(".show").innerHTML = "You search: " + document.querySelector("input").value;
   }
</script>
</body>
</html>

Output

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

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements