HTML onsearch Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Events & Handlers

Introduction

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.

What You’ll Learn

01

Event handler

Inline JS.

02

search event

Enter / clear.

03

type=search

Search input.

04

vs oninput

Commit vs live.

05

Clear button

Also triggers.

06

addEventListener

Preferred way.

Purpose of onsearch Attribute

The 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.

💡
Not the same as live search

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.

📝 Syntax

Set onsearch on an <input type="search"> or assign element.onsearch in JavaScript:

onsearch.html
<input type="search" id="q" onsearch="performSearch()">

<script>
  q.onsearch = performSearch;
  q.addEventListener("search", performSearch);
</script>

Syntax Rules

  • Value is JavaScript executed when the search event fires.
  • Only meaningful on <input type="search">.
  • Fires when the user presses Enter or clears the search field.
  • Does not fire on every keystroke — use oninput for that.
  • Also assignable as element.onsearch in script.
  • Modern alternative: addEventListener("search", handler).
  • Read the query with inputElement.value inside your handler.

💎 Values

The onsearch attribute accepts a string of JavaScript code:

  • onsearch="performSearch()" — Call a named function.
  • onsearch="filterResults()" — Filter a list on commit.
  • JavaScript: searchInput.onsearch = () => { … } — property assignment.
onsearch-handler.html
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);

⚡ Quick Reference

Property / APIWhen it runsNotes
searchEnter or clear on type=searchonsearch
inputEvery value changeLive typing — oninput
changeCommitted changeBlur for text — onchange
input.valueRead anytimeCurrent search query
type="search"RequiredSearch event target

Applicable Elements

ElementSupported?Notes
<input type="search">YesPrimary and intended use
<input type="text">NoNo search event — use Enter key or form submit
<textarea>NoNot a search control
<form>NoUse submit event on the form instead

onsearch vs oninput vs onchange

HandlerWhen it firesTypical use
onsearchEnter or clear on type=searchSubmit search, fetch results
oninputEvery keystroke / pasteLive autocomplete, instant filter
onchangeCommitted change (blur for text)Final validation
Form submitSubmit button or Enter in formFull form search with button

Examples Gallery

Inline search handler, dynamic assignment, and filtering a list when the user commits a search.

👀 Live Preview

Type a query and press Enter — or click the clear (×) button:

Waiting for search…

Example — onsearch on input type=search

Run a function when the user commits a search:

inline-onsearch.html
<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>
Try It Yourself

How It Works

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.

Dynamic Values with JavaScript

Assign the handler on an element at runtime:

dynamic-onsearch.html
<script>
  document.getElementById("dynamicSearch").onsearch = function () {
    var value = this.value.trim();
    log.textContent = value
      ? "You searched for: " + value
      : "Search cleared!";
  };
</script>
Try It Yourself

How It Works

Assigning element.onsearch is equivalent to the inline attribute. Use this.value inside the function to read the current query from the search input.

Example — Filter a list on search

Filter visible items only when the user commits the search:

filter-onsearch.html
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;
  });
});
Try It Yourself

How It Works

The handler runs once per search action, not on every character. For instant filtering while typing, combine with oninput or use oninput alone.

♿ Accessibility

  • Use a visible label — Pair type="search" with <label for="…"> or aria-label.
  • Announce results — Use aria-live="polite" on a region that updates after search.
  • Keyboard support — Enter must work; do not rely only on mouse clicks.
  • Clear action — When search is cleared, restore the full result set and announce it.
  • Do not trap focus — After search, keep tab order logical for screen reader users.

🧠 How onsearch Works

1

User types query

Keystrokes only.

Input
2

Enter or clear

Commits search.

Action
3

search fires

onsearch runs.

Event
=

Run search logic

Filter, fetch, navigate.

Browser Support

The search event and onsearch handler are supported in all modern browsers on input type="search", including Chrome, Firefox, Safari, and Edge.

HTML5 · Fully supported

Universal onsearch support

All major browsers fire search on type=search inputs when the user presses Enter or clears the field.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer Limited
Partial support
Opera Fully supported
Full support
onsearch attribute 99% supported

Bottom line: Reliable on input type="search" in all modern browsers.

💡 Best Practices

✅ Do

  • Use type="search" for search fields that fire the search event
  • Trim and validate input.value before fetching or filtering
  • Handle the cleared state — restore full results when the field is empty
  • Use addEventListener("search", …) in production
  • Pair with labels and aria-live for accessible result updates

❌ Don’t

  • Expect onsearch on type="text" inputs
  • Use onsearch for live autocomplete — use oninput
  • Block Enter key default behavior without good reason
  • Rely on alert() for user feedback in demos or production
  • Confuse search commit with form submit on multi-field forms

Conclusion

The 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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onsearch

Bookmark these before wiring search handlers.

5
Core concepts
02

Enter / clear

Triggers event.

When
03

Not oninput

Commit only.

Compare
📝 04

.value

Read query.

API
05

aria-live

Announce results.

A11y

❓ Frequently Asked Questions

It runs JavaScript when the search event fires on an input type="search" — when the user presses Enter or clears the field.
No. It fires only when the user commits the search (Enter) or clears the field. For live typing, use oninput.
No. The search event is for type="search". On text inputs, use form submit or listen for the Enter key.
Browsers fire the 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.
Yes in all modern browsers on input type="search". Internet Explorer has limited support for the search event.

Build interactive search fields

Practice the onsearch attribute with inline handlers and list filtering in the Try It editor.

Try search input example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful