HTML onsearch Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The onsearch attribute is an event handler for the search event on <input type="search">. In supporting browsers it runs when the user initiates a search — for example pressing Enter — or clears the field with the built-in clear control.

Read the query from this.value (or event.target.value). For portable live filtering, prefer oninput / onchange or form onsubmit.

Non-standard.

MDN marks the search event as not standardized. It is missing in Firefox. Study it for Chromium/WebKit behavior; ship portable listeners for production.

Related non-standard: incremental.

On WebKit/Blink, incremental can fire debounced search events while typing. Do not rely on it cross-browser.

HTML Syntax

syntax.html
<input type="search" onsearch=" handler">

Modern equivalent (where supported): input.addEventListener("search", function (event) { … });. Portable alternative: listen for input or change.

Attribute Values

The value is a script (event handler code). Typical triggers in supporting UAs:

TriggerWhat happensTry it
Enter / Returnsearch fires with the current valueTry it
Clear controlsearch fires; value is usually ""Try it
incrementalDebounced search while typing (WebKit/Blink)Try it
Portable inputWorks everywhere for live updatesTry it

Live Preview

Type a query and press Enter (or use the clear control if shown). The log reports search when this browser supports it.

Search field

Support is detected below. If search is missing, use the portable input path in Example 3.

Log

Checking support…

HTML Examples

Short demos. In Chromium/Safari, press Enter or clear the field. Firefox may need Example 3.

Example 1 — Log the Search Term

Show the value when search fires (Enter or clear).

HTML
<label for="q">Search</label>
<input id="q" type="search" name="q"
  onsearch="document.getElementById('out').textContent =
    'Search: ' + JSON.stringify(this.value)">
<p id="out">Waiting…</p>
Try It Yourself

How It Works

  1. type="search" enables search UI (and the event where supported).
  2. User presses Enter or clears the field.
  3. onsearch runs with the current value.
  4. Clear usually reports an empty string.

Example 2 — Live Updates with incremental

Non-standard WebKit/Blink attribute for debounced search-while-typing.

HTML
<label for="live">Live search</label>
<input id="live" type="search" name="q" incremental
  onsearch="document.getElementById('hits').textContent =
    'Query: ' + this.value">
<p id="hits">Type slowly in Chrome/Safari…</p>
Try It Yourself

How It Works

  1. incremental asks the UA for live search events.
  2. Typing pauses — a debounced search may fire.
  3. Handler updates results from this.value.
  4. Not in Firefox — use input for portability.

Example 3 — Portable input (+ optional search)

Standard pattern: always listen to input; enhance with search where it exists.

HTML
<label for="q2">Search</label>
<input id="q2" type="search" name="q">
<p id="status">Waiting…</p>
<script>
  var el = document.getElementById("q2");
  function show(source) {
    document.getElementById("status").textContent =
      source + ": " + JSON.stringify(el.value);
  }
  el.addEventListener("input", function () { show("input"); });
  el.addEventListener("search", function () { show("search"); });
</script>
Try It Yourself

How It Works

  1. input updates on every change — works in all modern browsers.
  2. search adds Enter/clear behavior where supported.
  3. Same UI (type="search") for the clear control and search keyboard.
  4. Production tip: treat search as optional enhancement.

Edge Cases & Pitfalls

The main risk is assuming search exists everywhere.

firefox

No search event in Firefox

Enter and typing will not fire onsearch there. Use input / form submit.

type

Only meaningful on type="search"

Other input types do not get this search UX or event.

incremental

incremental is also non-standard

Live-as-you-type via search is WebKit/Blink-oriented. Prefer input + debounce.

cancel

Event is not cancelable

You cannot preventDefault() the search event to block it.

form

Enter may also submit a form

Inside a <form>, handle submit if navigation or a GET search is intended.

Browser Support

onsearch / the search event is a non-standard HTMLInputElement feature. It is not a Node.js, Deno, or Bun API.

✓ Non-standard

onsearch

Available in Chromium-based browsers and historically Safari/WebKit. Not supported in Firefox. Prefer input/change/submit for cross-browser apps.

~78% Global support (approx.)
Google Chrome Yes
Supported
Mozilla Firefox No
Not supported
Apple Safari Partial*
Historically yes; verify current
Microsoft Edge 79+
Supported (Chromium)
Opera Yes
Supported
Samsung Internet Yes
Supported
Internet Explorer No
Not supported
onsearch Partial / non-standard

Bottom line: Fine to study and enhance Chromium/WebKit UIs. Do not rely on onsearch alone for production search boxes.

Key Takeaways

  • Role: handle search initiation on type="search".
  • Non-standard — missing in Firefox.
  • Prefer input / form submit for portability.
  • incremental is also non-standard (WebKit/Blink).

One line: onsearch is a non-standard Enter/clear hook for search inputs — use input when you need every browser.

Frequently Asked Questions

It is an event handler for the search event on <input type="search">. Supporting browsers fire it when the user initiates a search (for example pressing Enter) or clears the field with the clear control.
No. MDN marks the search event as non-standard. It is not in Firefox. Prefer oninput, onchange, or form submit for portable code.
Typically on Enter while focused, when using the clear (×) control (value becomes ""), and — if the non-standard incremental attribute is set — after typing pauses in WebKit/Blink.
A non-standard WebKit/Blink attribute. When present, the browser sends debounced search events while the user types for live filtering.
Use <input type="search"> for UX, then listen with addEventListener("input", …) and/or submit the form. Optionally add search only as an enhancement where supported.
No. The search event is not cancelable according to MDN.

Next: onseeked

Run code when media seeking has completed.

onseeked tutorial →

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.

8 people found this page helpful