onsearch
The onsearch event in HTML is activated when a user starts a search within an input field, enabling scripts to respond to search activities.
onsearch
The onsearch
event attribute in HTML is triggered when a user submits a search query in an <input>
element with the type set to search
. This event enables developers to run specific actions or scripts in response to the user's search, improving the search functionality.
Syntax
In HTML
index.html
<element onsearch="myScript">
In JS
script.js
object.onsearch = function(){myScript};
addEventListener()
function is used. In JavaScript, the
script.js
object.addEventListener("search", myScript);
Example
::event-onsearch
::
index.html
<!DOCTYPE html>
<html>
<body>
<p>Write something in the search field and press "ENTER".</p>
<input type="search" id="myInput" onsearch="myFunction()">
<p><strong>Note:</strong> The onsearch event is not supported in Internet Explorer, Firefox or Opera 12 and earlier versions.</p>
<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>
Values
Conclusion
The onsearch
event allows developers to capture and respond to search queries in <input>
fields. It provides an efficient way to handle user-initiated search actions.