jQuery error Event

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

What Is the error Event?

The error event fires on elements (commonly <img>) when the browser fails to load that resource. Bind with .on("error", handler) (since 1.7) and trigger with .trigger("error").

Remember
$("#book")
  .on("error", function () {
    // resource failed to load
  })
  .attr("src", "missing.png");  // bind BEFORE setting src

// Trigger for tests:
$("#book").trigger("error");

// Do NOT: $(window).on("error", …) for script errors
NeedUse
Broken image / resource load$("img").on("error", fn)
Successful resource load.on("load", fn)
Script exceptionswindow.onerror (not jQuery)
Ajax HTTP failureajaxError / .fail()
Legacy shorthand.error() — removed in 3.0

How to Use the error Event

Bind first, set src second, and keep fallbacks loop-safe.

  1. Select the element (usually an <img>).
  2. Attach .on("error", handler) before changing src.
  3. Set src (or let the browser load a broken URL).
  4. In the handler, swap a valid fallback, log, or update UI.
  5. Prefer .one("error") or a guaranteed-valid fallback to avoid loops.
PieceRequired?Role
handlerYesfunction(event) — runs when load fails
Bind orderCritical.on("error") before .attr("src")
.trigger("error")OptionalFire handlers without a real failure
.one("error")OptionalRun once — safer for fallbacks

Pseudocode

Pseudocode
select image element
bind .on("error", handler)     // must happen first
set image.src = url

when browser fails to load url:
    fire error event on the element
    call each registered handler(event)

if handler sets a new src that also fails:
    error fires again  → risk of infinite loop
    prefer .one("error") or a valid fallback

Cheat Sheet

Keep these facts handy while wiring image fallbacks.

TopicDetail
Bind$(el).on("error", handler)
Unbind$(el).off("error" [, handler])
Once$(el).one("error", handler)
Trigger$(el).trigger("error")
Since.on("error") 1.7 · .trigger("error") 1.0
Typical target<img> (and other loaded resources)
Protocol tipMay not fire reliably under file: — use http(s)
Removed API.error() removed in jQuery 3.0

Live Preview

Bind an error handler on a demo image, then load a missing URL or fire trigger("error").

Demo image below updates from the handler. Missing URLs need network/HTTP (not file:).

Demo image for error event

Result
Ready — pick an action and click Run.

jQuery Examples

Short, copy-ready snippets based on the official API demos. Prefer text logs over alert() in real apps.

Example 1 — Bind Before Setting src

Official pattern: attach error first, then load a missing image.

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

How It Works

  1. Bind. .on("error") registers the handler on #book.
  2. Load. .attr("src") starts the browser request.
  3. Fail. A missing URL causes the browser to fire error.
  4. Order. Binding first ensures the handler is ready in time.

Example 2 — Replace Broken Images

Official idea: swap src inside the handler — keep the fallback valid.

jQuery
var fallback =
  "data:image/svg+xml," +
  encodeURIComponent(
    '<svg xmlns="http://www.w3.org/2000/svg" width="96" height="64">' +
    '<rect fill="#e2e8f0" width="96" height="64"/>' +
    '<text x="48" y="38" text-anchor="middle" fill="#64748b" font-size="12">N/A</text>' +
    "</svg>"
  );

$("img.thumb")
  .on("error", function () {
    $(this).attr("src", fallback);
    $("#log").append(" Replaced. ");
  })
  .attr("src", "missing.png");
Try It Yourself

How It Works

  1. Fail. Each broken thumbnail fires error.
  2. Swap. $(this).attr("src", fallback) replaces the broken URL.
  3. Safe. A data: SVG cannot 404, so the handler does not loop.
  4. Risk. A bad file fallback would fire error again forever.

Example 3 — .trigger("error")

Official demo: fire the handler from a button without a real network failure.

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

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

How It Works

  1. Bind. The image already has an error handler.
  2. Click. The button calls .trigger("error").
  3. Synthetic. Handlers run without a failed network load.
  4. Use. Handy for demos and fallback UI tests.

Example 5 — .one("error") + SVG Fallback

Run once, swap to an inline SVG, and avoid infinite loops.

jQuery
var fallbackSvg =
  "data:image/svg+xml," +
  encodeURIComponent(
    '<svg xmlns="http://www.w3.org/2000/svg" width="96" height="96">' +
    '<rect fill="#e2e8f0" width="96" height="96"/>' +
    '<text x="48" y="54" text-anchor="middle" fill="#64748b" font-size="12">User</text>' +
    "</svg>"
  );

$("#avatar")
  .one("error", function () {
    $(this).attr("src", fallbackSvg);
    $("#log").text("Swapped to SVG fallback once.");
  })
  .attr("src", "missing-avatar.png");
Try It Yourself

How It Works

  1. Once. .one("error") auto-unbinds after the first call.
  2. Swap. The handler sets a data: SVG that always loads.
  3. Safe. No second failure → no infinite error loop.
  4. Use. Ideal for avatars and thumbnail placeholders.

Edge Cases & Pitfalls

Check these before shipping image fallback handlers.

order

Bind before src

Setting src first can miss a fast failure. Always attach .on("error") first.

loop

Bad fallbacks loop forever

If the replacement URL also fails, error fires again. Use .one() or a valid data: URI.

window

Not for script errors

Do not put jQuery .on("error") on window. Use window.onerror for exceptions.

file

file: may not fire

Local file: pages often skip HTTP-style failure reporting. Test over http(s).

Browser Support

The element error event is a long-standing browser event. Prefer .on("error") (jQuery 1.7+). The .error() shorthand was removed in jQuery 3.0.

✓ Stable · .on() 1.7+

jQuery error event

Works in every browser that loads images and runs your jQuery build. Prefer current jQuery 3.x and the .on() / .trigger() APIs.

100% Browser event
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 Universal

Bottom line: Safe for everyday apps. Bind before src, keep fallbacks valid, and never confuse this with window.onerror.

Performance Notes

ApproachCostBest for
One .on("error") per imageCheapSmall galleries / avatars
.one("error") + SVG fallbackCheapLoop-safe placeholders
Heavy work on every failureExpensiveAvoid — keep handlers tiny

Error handlers usually run rarely. Keep them light: swap a src, log an id, or show a short status — not heavy layout work.

Key Takeaways

  • Role: fires when a resource like an image fails to load.
  • Order: .on("error") before .attr("src").
  • Safe fallback: use .one() or a valid data: SVG.
  • Not window: script errors belong on window.onerror.

One line: jQuery’s error event is for failed resource loads — bind it before src, and keep fallbacks loop-safe.

Frequently Asked Questions

The error event is sent to elements such as images that the browser failed to load correctly. Bind with .on("error", handler) (since 1.7). It is a browser resource-load event — not an Ajax failure callback and not window.onerror for script exceptions.
The browser can fire error as soon as loading fails. If you set src before .on("error"), you may miss the event. Official pattern: chain .on("error", fn) first, then .attr("src", url).
error relies on HTTP-style load failure reporting. Pages opened as file: often behave differently. Test over http:// or https://.
No. Do not attach a jQuery error handler to window for script errors. The window error event uses different arguments and return-value rules. Use window.onerror (or window.addEventListener("error", …)) instead.
If the replacement src also fails, error fires again. Ensure the fallback is valid, or use .one("error") / .off("error") after swapping. Inline data: SVG placeholders are safe because they cannot 404.
No. The .error() shorthand was deprecated in 1.8 and removed in jQuery 3.0. Use .on("error", handler) and .trigger("error") instead.
Call $(element).trigger("error"). Bound handlers run without a real network failure — useful for testing fallback UI.

Next: .error() method

See the removed shorthand and how to migrate to .on("error").

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