👀 Live Preview
Type a query and press Enter — or click the clear (×) button:
Waiting for search…

The onsearch attribute is an inline event handler for the search event on <input type="search">. It runs JavaScript when the user commits a search — by pressing Enter or clicking the browser’s clear (×) button on the search field. Unlike oninput, it does not fire on every keystroke. Use it to fetch results, filter a list, or navigate when the user finishes typing their query. Prefer addEventListener("search", …) in production; inline onsearch is great for learning.
Inline JS.
Enter / clear.
Search input.
Commit vs live.
Also triggers.
Preferred way.
onsearch AttributeThe primary purpose of onsearch is to run custom JavaScript when the user performs a search action in a search input — filter results, fetch data from a server, update the UI, or log analytics for the submitted query.
The search event is designed for <input type="search">, which browsers may render with a built-in clear control. The event fires when the user confirms their search, not while they are still typing.
For filtering on every keystroke, use oninput or addEventListener("input", …). Use onsearch when you only want to act after the user presses Enter or clears the field.
Set onsearch on an <input type="search"> or assign element.onsearch in JavaScript:
<input type="search" id="q" onsearch="performSearch()">
<script>
q.onsearch = performSearch;
q.addEventListener("search", performSearch);
</script>search event fires.<input type="search">.oninput for that.element.onsearch in script.addEventListener("search", handler).inputElement.value inside your handler.The onsearch attribute accepts a string of JavaScript code:
onsearch="performSearch()" — Call a named function.onsearch="filterResults()" — Filter a list on commit.searchInput.onsearch = () => { … } — property assignment.function performSearch() {
var query = document.getElementById("searchInput").value.trim();
if (query) {
result.textContent = "Searching for: " + query;
} else {
result.textContent = "Search cleared — showing all results";
}
}
document.getElementById("searchInput").addEventListener("search", performSearch);| Property / API | When it runs | Notes |
|---|---|---|
search | Enter or clear on type=search | onsearch |
input | Every value change | Live typing — oninput |
change | Committed change | Blur for text — onchange |
input.value | Read anytime | Current search query |
type="search" | Required | Search event target |
| Element | Supported? | Notes |
|---|---|---|
<input type="search"> | Yes | Primary and intended use |
<input type="text"> | No | No search event — use Enter key or form submit |
<textarea> | No | Not a search control |
<form> | No | Use submit event on the form instead |
onsearch vs oninput vs onchange| Handler | When it fires | Typical use |
|---|---|---|
onsearch | Enter or clear on type=search | Submit search, fetch results |
oninput | Every keystroke / paste | Live autocomplete, instant filter |
onchange | Committed change (blur for text) | Final validation |
Form submit | Submit button or Enter in form | Full form search with button |
Inline search handler, dynamic assignment, and filtering a list when the user commits a search.
Type a query and press Enter — or click the clear (×) button:
Waiting for search…
Run a function when the user commits a search:
<input type="search" id="searchInput"
placeholder="Search here…"
onsearch="performSearch()">
<p id="result"></p>
<script>
function performSearch() {
var q = document.getElementById("searchInput").value.trim();
document.getElementById("result").textContent =
q ? "You searched for: " + q : "Search cleared";
}
</script>When the user presses Enter or clears the search field, the browser fires the search event and runs performSearch(). Typing alone does not trigger it — only the committed search action does.
Assign the handler on an element at runtime:
<script>
document.getElementById("dynamicSearch").onsearch = function () {
var value = this.value.trim();
log.textContent = value
? "You searched for: " + value
: "Search cleared!";
};
</script>Assigning element.onsearch is equivalent to the inline attribute. Use this.value inside the function to read the current query from the search input.
Filter visible items only when the user commits the search:
searchInput.addEventListener("search", function () {
var q = this.value.trim().toLowerCase();
items.forEach(function (item) {
var match = !q || item.textContent.toLowerCase().includes(q);
item.hidden = !match;
});
});The handler runs once per search action, not on every character. For instant filtering while typing, combine with oninput or use oninput alone.
type="search" with <label for="…"> or aria-label.aria-live="polite" on a region that updates after search.Keystrokes only.
Commits search.
onsearch runs.
Filter, fetch, navigate.
The search event and onsearch handler are supported in all modern browsers on input type="search", including Chrome, Firefox, Safari, and Edge.
onsearch supportAll major browsers fire search on type=search inputs when the user presses Enter or clears the field.
Bottom line: Reliable on input type="search" in all modern browsers.
type="search" for search fields that fire the search eventinput.value before fetching or filteringaddEventListener("search", …) in productionaria-live for accessible result updatesonsearch on type="text" inputsonsearch for live autocomplete — use oninputalert() for user feedback in demos or productionThe onsearch attribute runs JavaScript when the user commits a search on input type="search" — pressing Enter or clearing the field — useful for fetching results, filtering lists, and building interactive search UIs.
Use oninput for live typing feedback, use addEventListener in production, and always provide accessible labels and result announcements.
onsearchBookmark these before wiring search handlers.
Required input.
ScopeTriggers event.
WhenCommit only.
CompareRead query.
APIAnnounce results.
A11ysearch event fires on an input type="search" — when the user presses Enter or clears the field.oninput.type="search". On text inputs, use form submit or listen for the Enter key.search event when the built-in clear (×) control is used. Your handler should treat an empty value as “show all results.”addEventListener("search", handler) is preferred in production — easier to attach multiple listeners and keep HTML separate from behavior.input type="search". Internet Explorer has limited support for the search event.Practice the onsearch attribute with inline handlers and list filtering in the Try It editor.
5 people found this page helpful