jQuery .error() Method

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

What Is .error()?

.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
NeedUse
New code (bind).on("error", fn)
New code (trigger).trigger("error")
Legacy bind.error(fn) ⚠ removed 3.0
Legacy trigger.error() ⚠ removed 3.0
Script errorswindow.onerror (not jQuery)

How to Use .error()

Recognize the legacy API in old code, then migrate to .on() / .trigger().

  1. Bind with .error(handler) (legacy) or .on("error", handler) (preferred).
  2. Attach the handler before setting src (or another resource URL).
  3. Optionally pass eventData: .error(data, handler) → .on("error", data, handler).
  4. Trigger with no-arg .error() (legacy) or .trigger("error") (preferred).
  5. Never attach jQuery error handlers to window for script errors.
PieceRequired?Role
handlerFor bind formfunction(event) — runs on load failure
eventDataOptional (1.4.3+)Extra data available as event.data
Attach orderYesBind first, then set src
Fallback URLIf swappingMust 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")

Cheat Sheet

Quick map from the removed shorthand to modern APIs.

TopicDetail
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 / gone1.0 · deprecated 1.8 · removed 3.0
Typical target<img> (and other loadable resources)
Not forwindow script errors → use window.onerror

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.

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.

jQuery
$("#book")
  .error(function () {
    $("#out").text("Handler for error called.");
  })
  .attr("src", "missing.png");
Try It Yourself

How It Works

  1. Bind. .error(fn) registers the handler on #book.
  2. Src. Setting src starts the network load.
  3. Fail. A missing resource fires the browser error event.
  4. Handle. The callback updates the status text.

Example 2 — .error(eventData, handler)

Pass extra data (since 1.4.3). Modern form: .on("error", data, fn).

jQuery
$("#avatar").error({ label: "avatar" }, function (event) {
  $("#out").text("Failed: " + event.data.label);
  $(this).attr("src", "fallback.png");
});
Try It Yourself

How It Works

  1. Data. The object becomes event.data inside the handler.
  2. Fail. A bad src triggers error.
  3. Label. Read event.data.label for context.
  4. Fallback. Swap to a URL that is known to load.

Example 3 — No-arg .error() Trigger

Fire the event programmatically. Prefer .trigger("error") today.

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

$("#other").on("click", function () {
  $("#book").error(); // legacy trigger — use .trigger("error")
});
Try It Yourself

How It Works

  1. Bind. The handler is registered first.
  2. Click. No-arg .error() triggers the event.
  3. Run. Bound handlers execute immediately.
  4. 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");
Try It Yourself

How It Works

  1. Removed. Calling .error() in 3.x throws a TypeError.
  2. Replace. Use .on("error", fn) for binding.
  3. Same event. The browser error event is unchanged.
  4. Order. Still bind before setting src.

Example 5 — Chain Bind, Then Set src

Official pattern: one chain for several images — attach first, then assign src.

jQuery
$("img.thumb")
  .error(function () {
    $(this).attr("src", "fallback.png");
  })
  .attr("src", "missing.png");
Try It Yourself

How It Works

  1. Select. All img.thumb elements get the handler.
  2. Bind first. .error(fn) runs before any load starts.
  3. Src. Shared .attr("src", …) starts each load.
  4. 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.

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 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: Do not ship .error() on jQuery 3. Migrate bindings to .on('error') and triggers to .trigger('error'). Test over http(s), not file:.

Performance Notes

ApproachCostBest for
Direct .on("error") on each imageCheapBroken-image fallbacks
Delegation for errorUnreliableAvoid — error does not bubble
Infinite fallback swapsExpensiveAvoid — 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.

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.

Next: resize event

Learn how to listen for viewport size changes with .on("resize").

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.

8 people found this page helpful