jQuery error Event

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

Definition

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

Syntax

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

jQuery
$("#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
CallWhat it does
errorLearn jQuery error event: bind .on(\

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

Parameters

ParameterDescription
Step 1Select the element (usually an <img>).
Step 2Attach .on("error", handler) before changing src.
Step 3Set src (or let the browser load a broken URL).

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

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.

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.

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.

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