jQuery .error() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Browser Events

Definition

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

Syntax

Start with these common calls. Keep them short and readable.

jQuery
// 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
CallWhat it does
handlerfunction(event) — runs on load failure
eventDataExtra data available as event.data
Attach orderBind first, then set src
Fallback URLMust load successfully or you loop forever

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Bind with .error(handler) (legacy) or .on("error", handler) (preferred).
Step 2Attach the handler before setting src (or another resource URL).
Step 3Optionally pass eventData: .error(data, handler) → .on("error", data, handler).

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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.

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: load Event

Learn how to run code when windows, images, and iframes finish loading with .on("load").

load Event tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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