.error() was jQuery’s legacy shorthand (1.0–2.x) for binding or triggering the browser error event on elements such as images. It was deprecated in 1.8 and removed in jQuery 3.0. Modern code uses .on("error") / .trigger("error").
Remember
// Removed in jQuery 3.0 (worked in 1.x / 2.x)
$("#book").error(function () {
// resource failed to load
}).attr("src", "missing.png");
// Modern replacements
$("#book").on("error", handler); // bind
$("#book").trigger("error"); // fire
// Attach BEFORE setting src — or you may miss the event
Trigger with no-arg .error() (legacy) or .trigger("error") (preferred).
Never attach jQuery error handlers to window for script errors.
Piece
Required?
Role
handler
For bind form
function(event) — runs on load failure
eventData
Optional (1.4.3+)
Extra data available as event.data
Attach order
Yes
Bind first, then set src
Fallback URL
If swapping
Must load successfully or you loop forever
Pseudocode
Pseudocode
// Legacy (jQuery 1.x / 2.x only)
call element.error(handler)
→ bind "error" on element
call element.error()
→ trigger "error" on element
// Modern (jQuery 1.7+)
call element.on("error", handler)
set element.src = url
when browser fails to load url:
fire "error" on element
call handler(event)
to migrate:
.error(fn) → .on("error", fn)
.error(data, fn) → .on("error", data, fn)
.error() → .trigger("error")
Reference
Cheat Sheet
Quick map from the removed shorthand to modern APIs.
Topic
Detail
Legacy bind
$("img").error(handler) ⚠ removed 3.0
Modern bind
$("img").on("error", handler)
Legacy trigger
$("img").error() ⚠ removed 3.0
Modern trigger
$("img").trigger("error")
Unbind
$("img").off("error" [, handler])
Since / gone
1.0 · deprecated 1.8 · removed 3.0
Typical target
<img> (and other loadable resources)
Not for
window script errors → use window.onerror
Try it
Live Preview
This page loads jQuery 3.x, so .error() is gone. The demo uses the modern equivalent .on("error").
Live demo uses .on("error") because .error() was removed in jQuery 3.0.
Result
Ready — pick an action and click Run.
Code
jQuery Examples
Legacy snippets show the removed API (jQuery 1.x/2.x). Prefer .on("error") for new projects.
Example 1 — Basic .error(handler)
Official legacy pattern: bind, then set a missing src.
Click #other → .error() fires the event
Bound handler runs without a real network failure
How It Works
Bind. The handler is registered first.
Click. No-arg .error() triggers the event.
Run. Bound handlers execute immediately.
Migrate. Replace with .trigger("error") in jQuery 3.x.
Example 4 — Migrate to .on("error")
In jQuery 3.x, $.fn.error is undefined — use .on() / .trigger().
jQuery
// Legacy — removed in jQuery 3.0
// $("#book").error(fn).attr("src", "missing.png");
// Modern — use for new code
$("#book")
.on("error", function () {
$("#out").text("Modern .on('error') handler ran.");
})
.attr("src", "missing.png");
Each broken thumb fires error
Handler swaps src to fallback.png
Fallback must exist — or you loop forever
How It Works
Select. All img.thumb elements get the handler.
Bind first..error(fn) runs before any load starts.
Src. Shared .attr("src", …) starts each load.
Swap. On failure, each image gets a safe fallback URL.
Edge Cases & Pitfalls
Watch for these when reading legacy code or migrating to jQuery 3.
removed
Gone in jQuery 3.0
.error() is not just deprecated — calling it in 3.x throws. Migrate to .on("error").
order
Attach before src
If you set src first, the failure can fire before your handler exists.
loop
Bad fallback loops forever
If the replacement image also 404s, error fires again. Use a known-good URL or .one("error").
window
Not for script errors
Do not bind jQuery error on window. Use window.onerror for uncaught exceptions.
Compatibility
Browser Support
The .error() method existed in jQuery 1.0–2.x. It was deprecated in 1.8 and removed in 3.0. The underlying browser error event still works — bind it with .on("error") in any supported jQuery version (1.7+).
✓ Removed · jQuery 3.0
jQuery .error()
Legacy method only. Use .on('error') / .trigger('error') on jQuery 3.x. The browser error event on images works in all modern browsers over http(s).
0%In jQuery 3.x
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.error()Removed
Bottom line: Do not ship .error() on jQuery 3. Migrate bindings to .on('error') and triggers to .trigger('error'). Test over http(s), not file:.
Analysis
Performance Notes
Approach
Cost
Best for
Direct .on("error") on each image
Cheap
Broken-image fallbacks
Delegation for error
Unreliable
Avoid — error does not bubble
Infinite fallback swaps
Expensive
Avoid — validate the fallback URL
Bind handlers directly on the elements that load resources. Keep the callback light — swap src or show a label, then stop.
Remember
Key Takeaways
Role: legacy bind/trigger for element load failures (images).
Migrate:.on("error", fn) and .trigger("error") — required on jQuery 3.
Order: always attach the handler before setting src.
Not window: use window.onerror for script errors, not jQuery error.
One line:.error() is removed sugar for .on("error") / .trigger("error") — migrate before jQuery 3.
Frequently Asked Questions
The .error() method had two roles. With a handler — .error(handler) or .error(eventData, handler) — it bound a function that runs when a resource (such as an image) fails to load. With no arguments — .error() — it triggered the error event. Both forms existed since jQuery 1.0; binding was deprecated in 1.8 and the entire method was removed in jQuery 3.0.
Both. Binding signatures were deprecated in jQuery 1.8. The entire .error() method — including the no-argument trigger — was removed in jQuery 3.0. Calling .error() in jQuery 3.x throws because $.fn.error is undefined. Use .on("error", handler) and .trigger("error") instead.
.error(handler) registers a callback and does not fire immediately. .error() with no arguments triggers the error event on each matched element — the same as .trigger("error"). Both were removed in jQuery 3.0.
Replace .error(fn) with .on("error", fn). Replace .error(data, fn) with .on("error", data, fn). Replace no-arg .error() with .trigger("error"). Cleanup with .off("error"). Always attach the handler before setting src.
Yes in jQuery 1.x and 2.x. No in jQuery 3.x — the method is gone. Legacy Try-it labs on this page use jQuery 1.12.4 so .error() exists. Projects on jQuery 3.x must use .on("error") and .trigger("error").
No. Do not use jQuery error handlers on window for script errors. window.onerror uses different arguments and return-value rules. Attach element error handlers to resources such as img, and use window.onerror for uncaught JavaScript exceptions.