jQuery .error() Method

Beginner
⚠️ Removed in jQuery 3.0
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Browser events

What You’ll Learn

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

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

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.

📝 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:

1. Bind a handler — .error( handler ) (since 1.0, deprecated 1.8, removed 3.0)

jQuery
.error( handler )
  • handler — function executed each time the error event fires: function( event ) { ... }.
  • Returns the jQuery object for chaining.
  • Modern replacement: .on( "error", handler ).

2. Bind with eventData — .error( [eventData], handler ) (since 1.4.3, deprecated 1.8, removed 3.0)

jQuery
.error( [eventData], handler )
  • eventData — optional object available in the handler as event.data.
  • Modern replacement: .on( "error", eventData, handler ).

3. Trigger the event — .error() (since 1.0, removed 3.0)

jQuery
.error()
  • No arguments — triggers error on every matched element.
  • Runs bound error handlers.
  • Modern replacement: .trigger( "error" ).

Official jQuery API migration note

jQuery
// Removed binding
$( "#book" ).error( function() {
  alert( "Handler for `error` called." );
} );

// Modern binding
$( "#book" ).on( "error", function() {
  alert( "Handler for `error` called." );
} );

// Removed trigger
$( "#book" ).error();

// Modern trigger
$( "#book" ).trigger( "error" );

Return value

  • All .error() signatures return the original jQuery object for chaining.
  • Inside the handler, this refers to the DOM element that failed to load — use $(this).attr("src", fallback) to swap broken images.

⚡ Quick Reference

GoalLegacy (.error)Modern replacement
Bind error handler on image$("#book").error(fn)$("#book").on("error", fn)
Pass data to handler$("#avatar").error({ label: "avatar" }, fn)$("#avatar").on("error", { label: "avatar" }, fn)
Attach before setting src.error(fn).attr("src", url).on("error", fn).attr("src", url)
Trigger error programmatically$("#book").error()$("#book").trigger("error")
Swap broken image fallback$(this).attr("src", "fallback.png") inside handler
Remove error handlers$("#book").off("error")
Delegated error on gallery images$("#gallery").on("error", "img", fn) — not available via .error()

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

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.

jQuery
$( "#book" )
  .error( function() {
    alert( "Handler for \`error\` called." );
  } )
  .attr( "src", "https://httpstat.us/404" );
Try It Yourself

How It Works

.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
var fallback = "https://via.placeholder.com/96/cccccc/666666?text=User";

$( "#avatar" ).error( { label: "avatar" }, function( event ) {
  $( "#out" ).text( "Failed: " + event.data.label + " → swapping fallback" );
  $( this ).attr( "src", fallback );
} );

$( "#load" ).on( "click", function() {
  $( "#avatar" ).attr( "src", "https://httpstat.us/404" );
} );
Try It Yourself

How It Works

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.

jQuery
$( "#book" ).error( function() {
  $( "#out" ).text( "Handler for error called (via .error() trigger)." );
} );

$( "#other" ).on( "click", function() {
  $( "#book" ).error();
} );
Try It Yourself

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.

jQuery
// jQuery 3.7.1 — .error() is removed
if ( typeof $.fn.error === "undefined" ) {
  console.log( "$.fn.error is undefined in jQuery 3.x" );
}

$( "#legacy" )
  .on( "error", function() {
    $( "#log" ).append( "<div>Modern .on('error') handler ran</div>" );
    $( this ).attr( "src", "https://via.placeholder.com/100/cccccc/666666?text=OK" );
  } )
  .attr( "src", "https://httpstat.us/404" );
Try It Yourself

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.

jQuery
var fallback = "https://via.placeholder.com/90/cccccc/666666?text=—";
var count = 0;

$( "img.thumb" )
  .error( function() {
    count++;
    $( this ).attr( "src", fallback );
    $( "#log" ).text( "Legacy .error() swapped " + count + " broken image(s)." );
  } )
  .attr( "src", "https://httpstat.us/404" );
Try It Yourself

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.

🚀 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").
  • Shared eventData$("#avatar").error({ label: "avatar" }, fn) passes metadata without closures.
  • 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.

📝 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.
  • Ensure fallback images exist — otherwise error loops indefinitely after swapping src.
  • 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.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All 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.

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.

💡 Best Practices

✅ Do

  • Use .on("error", handler) for all new error bindings in jQuery 3.x
  • Use .trigger("error") instead of no-arg .error()
  • Migrate .error(fn) to .on("error", fn) before upgrading to jQuery 3.x
  • Chain .on("error", fn) before .attr("src", url)
  • Read the modern error event tutorial for fallback and delegation patterns

❌ Don’t

  • 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

Key Takeaways

Knowledge Unlocked

Six things to remember about .error()

Legacy bind, removed in 3.0.

6
Core concepts
data 02

eventData

Since 1.4.3

Handler
() 03

.error()

Trigger

No args
.on 04

.on("error")

Modern

Replace
05

.trigger()

Fire

Programmatic
3.0 06

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().

Next: jQuery Resize Event

React when the browser window changes size — bind on $(window) and debounce layout code.

resize event 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.

6 people found this page helpful