The .error() method is jQuery’s legacy shorthand for binding and triggering the error event on elements such as images. This tutorial covers .error(handler) since 1.0, .error(eventData, handler) since 1.4.3, no-argument .error() as a trigger, migration to .on("error") and .trigger("error"), and why the entire method was removed in jQuery 3.0 after being deprecated since 1.8. See the jQuery error event tutorial for the modern API.
01
.error(fn)
Bind handler
02
eventData
Since 1.4.3
03
.error()
Trigger event
04
.on()
Modern bind
05
.trigger()
Modern fire
06
Removed
jQuery 3.0
Fundamentals
Introduction
Before jQuery 1.7 unified events with .on() and .off(), every common event had a matching shorthand — .error(), .load(), .click(), and more. The .error() method let you catch broken images in one short call: $("#book").error(function(){ alert("Handler for error called."); }).attr("src", "missing.png");. It was deprecated since jQuery 1.8 and removed entirely in jQuery 3.0.
Understanding .error() matters when reading older image-fallback and gallery tutorials in legacy codebases. The method did two distinct jobs: with a function argument it binds; with no arguments it triggers. Modern jQuery splits those roles explicitly — .on("error", handler) for binding and .trigger("error") for firing.
⚠️
Attach before src
The jQuery .error() method must be chained before setting src. If the browser fails to load an image before your handler exists, the event is missed. Chain .error(fn) first, then .attr("src", url) — the same rule applies to modern .on("error", fn).
Concept
Understanding the .error() Method
The official jQuery API describes .error() as a way to bind an event handler to the error event, or trigger that event on an element. When you pass a handler, jQuery registers it on each matched element and calls it when the browser fails to load a resource — the same error event that .on("error") handles today on images, scripts, and other loadable elements.
When you call .error() with no arguments, jQuery synthetically fires the error event on each matched element. Bound handlers run once per call. This trigger behavior is equivalent to .trigger("error").
⚠️
Removed in jQuery 3.0
Do not use .error(handler), .error(eventData, handler), or no-argument .error() in jQuery 3.x — $.fn.error is undefined. Replace binding with .on("error", handler) or .on("error", eventData, handler). Replace triggering with .trigger("error"). For broken image fallbacks, delegation, and the full modern error workflow, read the jQuery error event tutorial.
Foundation
📝 Syntax
The .error() method has three signatures from the official jQuery API. All three were removed in jQuery 3.0 — use the modern replacements below:
$("#gallery").on("error", "img", fn) — not available via .error()
Compare
📋 .error() vs .on("error") vs .trigger("error") vs .off("error")
Four related APIs — know which one binds, which one fires, and which one removes handlers.
.error()
removed
Legacy bind (.error(fn)) or trigger (.error()) — removed in jQuery 3.0; use only when reading old code
.on("error")
bind
Modern binding since 1.7 — supports eventData, delegation on dynamic gallery images, attach-before-src chaining
.trigger("error")
fire
Programmatically run handlers — clearer than no-arg .error(); does not simulate a real network failure
.off("error")
remove
Unbind handlers — pair with .on("error") when cleaning up or preventing infinite fallback loops
Hands-On
Examples Gallery
Five examples cover binding on #book, eventData fallbacks, triggering, migration from .error() to .on("error"), and chaining on multiple images. Use the Try-it links to run each snippet in the browser. Examples 1–3 and 5 use jQuery 1.12.4 because .error() was removed in jQuery 3.0.
📚 Binding & Triggering
Legacy .error() signatures for binding handlers and firing events programmatically.
Example 1 — Bind Handler with .error(handler) on #book
The canonical legacy pattern — alert when a broken image fails to load. Handler attached before src is set.
.error(fn) registers the function on the element before .attr("src", ...) runs. jQuery internally routed this to the same event system as .on("error", fn). The handler runs when the browser reports the load failure.
Example 2 — Pass eventData with .error(eventData, handler)
Since jQuery 1.4.3, pass metadata before the handler — swap to a fallback when the avatar fails to load.
jQuery stores the { label: "avatar" } object and injects it into event.data when error fires. Inside the handler, $(this) is the failed image — set a valid fallback src to avoid the broken-icon UX. Modern equivalent: .on("error", { label: "avatar" }, fn).
Example 3 — Trigger Error with No-Argument .error()
Calling .error() without a handler fires the error event programmatically — bound handlers run once per call.
Image loads normally with valid src
Click Trigger with .error() → $("#book").error() → handler runs
No-arg .error() is trigger shorthand — prefer .trigger("error")
Removed in jQuery 3.0 along with binding form
How It Works
The first .error(function(){ ... }) binds a handler on #book. The button calls $("#book").error() with no arguments — that synthetically fires error. Replace with $("#book").trigger("error") for clarity in jQuery 3.x.
📈 Migration & Chaining
Compare legacy and modern APIs, and chain .error(handler) on multiple images.
Example 4 — Migration: jQuery 3.x Uses .on("error") Only
In jQuery 3.7.1, $.fn.error is undefined — only .on("error") works for binding.
Log: $.fn.error is undefined in jQuery 3.x ✓
Broken src → .on("error") handler runs → fallback image appears
Legacy .error(fn) would throw TypeError in jQuery 3.x
How It Works
jQuery 3.0 removed all event shorthands that conflicted with native properties or were superseded by .on(). The error event still works — only the .error()method is gone. Migration is a straight rename: .error(fn) → .on("error", fn), .error() → .trigger("error").
Example 5 — Chain .error(handler) on Multiple Images
Official API pattern — attach handler before src on every matched thumbnail in one chain.
Three thumbnails request broken URLs → error fires on each
Each handler swaps src to gray placeholder
Log: "Legacy .error() swapped 3 broken image(s)."
Same attach-before-src rule applies to .on("error")
How It Works
One .error(fn) call binds the same handler to every img.thumb in the collection. Chaining .attr("src", ...) after binding sets the same broken URL on all three — each fires error independently. The same pattern works with .on("error", fn).attr("src", url) in jQuery 3.x.
Applications
🚀 Common Use Cases
Reading legacy code — recognize $("#book").error(fn) as equivalent to .on("error", fn) in older image-fallback scripts.
Broken image placeholders — legacy tutorials bind .error() to swap dead CDN links with default avatars.
Programmatic trigger — $("#book").error() from a test button — replace with .trigger("error").
Gallery thumbnails — chained .error(fn).attr("src", url) on multiple images in one call.
Maintaining legacy jQuery 1.x sites — many older panels still use .error() — know how to migrate before upgrading to jQuery 3.x.
🧠 How .error() Routed Through jQuery
1
You call .error()
With a function argument, jQuery treats it as a bind request. With no arguments, it treats it as a trigger request. Both paths existed in jQuery 1.x and 2.x only.
dispatch
2
Bind path
.error(handler) delegated internally to the same event system as .on("error", handler) — handler stored before src is set.
.on("error")
3
Trigger path
No-arg .error() delegated to .trigger("error") — bound handlers run without a real network failure.
.trigger("error")
4
🖼
Removed in jQuery 3.0
The entire method is gone — use .on("error", fn).attr("src", url) for binding and .trigger("error") for firing. The attach-before-src rule carries over unchanged.
Important
📝 Notes
Removed in jQuery 3.0 — do not use .error(handler), .error(eventData, handler), or no-arg .error() in jQuery 3.x. Use .on("error") and .trigger("error") instead.
Deprecated since jQuery 1.8; binding form available since 1.0; eventData form since 1.4.3.
Attach the handler before setting src — the official jQuery API requirement for both legacy and modern APIs.
.error() does not support event delegation — use .on("error", "img", fn) for dynamic galleries.
The file: protocol often does not fire error reliably — test over HTTP/HTTPS.
Do not confuse element error with window.onerror for JavaScript exceptions.
Remove handlers with .off("error") — not by calling .error(undefined).
For the full modern error guide — fallbacks, delegation, and .one("error") — see the jQuery error event tutorial.
Compatibility
Browser Support
The underlying error event fires on resource elements such as <img> in browsers jQuery targets. The .error() method itself worked in jQuery 1.x and 2.x only — it was deprecated since 1.8 and removed in 3.0. Use .on("error") and .trigger("error") in jQuery 3.x.
✓ jQuery 1.0–2.x
jQuery .error() method
Available in jQuery 1.x and 2.x only. Removed entirely in jQuery 3.0 — unlike .keydown() which still works in 3.x. Native equivalent for binding: element.addEventListener('error', fn). Native equivalent for trigger: .trigger('error') or dispatchEvent.
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: Safe only in legacy jQuery 1.x/2.x projects. Migrate all .error() calls to .on('error') and .trigger('error') before upgrading to jQuery 3.x.
Wrap Up
Conclusion
The jQuery .error() method is the legacy shorthand for binding and triggering error handlers on elements such as images. Use .error(handler) or .error(eventData, handler) to bind — both deprecated since 1.8 and removed in jQuery 3.0. Use no-argument .error() to trigger — also removed in 3.0, equivalent to .trigger("error").
For new code and jQuery 3.x projects, prefer .on("error", fn) for binding and .trigger("error") for programmatic firing. Always attach handlers before setting src. When you encounter .error() in older gallery and avatar scripts, recognize it, understand it, and migrate before upgrading jQuery. For fallbacks, delegation, and the complete modern error workflow, continue with the jQuery error event tutorial.
Write new image handlers with removed .error(handler)
Upgrade to jQuery 3.x without replacing every .error() call
Set src before binding — you may miss the error event entirely
Confuse .error(fn) (bind) with .error() (trigger)
Use .unbind("error") — it is also deprecated; use .off("error")
Confuse element error with Ajax .fail() or window.onerror
Summary
Key Takeaways
Knowledge Unlocked
Six things to remember about .error()
Legacy bind, removed in 3.0.
6
Core concepts
.err01
.error(fn)
Bind
Removed
data02
eventData
Since 1.4.3
Handler
()03
.error()
Trigger
No args
.on04
.on("error")
Modern
Replace
⚡05
.trigger()
Fire
Programmatic
3.006
Removed
jQuery 3.0
Migrate
❓ Frequently Asked Questions
The .error() method has two roles. With a handler argument — .error(handler) or .error(eventData, handler) — it binds a function that runs when the browser fails to load a resource on a matched element, such as a broken image. With no arguments — .error() — it triggers the error event on each matched element, running bound handlers. Both forms existed since jQuery 1.0; the binding form is deprecated since 1.8 and the entire method was removed in jQuery 3.0.
Both. The binding signatures .error(handler) and .error(eventData, handler) were deprecated since jQuery 1.8. The entire .error() method — including the no-argument trigger form — was removed in jQuery 3.0. Unlike .keydown(), which still works in jQuery 3.x, calling .error() in jQuery 3.x throws a TypeError because $.fn.error is undefined. Use .on('error', handler) and .trigger('error') instead.
.error(handler) registers a callback — it does not fire the event immediately. .error() with no arguments triggers the error event programmatically on every matched element, the same as .trigger('error'). The handler form binds; the no-arg form fires. Both were removed in jQuery 3.0 — migrate to .on('error') for binding and .trigger('error') for firing.
Replace .error(fn) with .on('error', fn). Replace .error(data, fn) with .on('error', data, fn). Replace no-arg .error() with .trigger('error'). Replace .unbind('error') cleanup with .off('error'). Chain .on('error', fn) before .attr('src', url) — the attach-before-src rule applies to the modern API exactly as it did to .error(handler).
Yes in jQuery 1.x and 2.x — .error(handler), .error(eventData, handler), and no-arg .error() all work. No in jQuery 3.x — the method was removed entirely. Legacy tutorials and Try-it labs on this page use jQuery 1.12.4 to demonstrate the old API. Any project on jQuery 3.x must use .on('error') and .trigger('error'); see the jQuery error event tutorial for the modern workflow.
Yes. Whether you use legacy .error(handler) or modern .on('error', handler), the browser fires the error event as soon as a resource fails to load. If you set src before binding the handler, the failure may happen before your callback exists and the event is missed. Always chain .error(fn) or .on('error', fn) first, then .attr('src', url) — the official jQuery API pattern for #book and broken images.
Did you know?
jQuery removed .error() in version 3.0 — a stronger fate than shorthands like .keydown(), which remain in jQuery 3.x as deprecated APIs. The removal happened because the method name collided with native DOM semantics and because .on("error") had fully replaced it since jQuery 1.7. If you see $(img).error(fn) in old tutorials, it targets the same browser error event on images — not Ajax .fail().