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
Bind first, set src second, and keep fallbacks loop-safe.
Select the element (usually an <img>).
Attach .on("error", handler) before changing src.
Set src (or let the browser load a broken URL).
In the handler, swap a valid fallback, log, or update UI.
Prefer .one("error") or a guaranteed-valid fallback to avoid loops.
Piece
Required?
Role
handler
Yes
function(event) — runs when load fails
Bind order
Critical
.on("error") before .attr("src")
.trigger("error")
Optional
Fire handlers without a real failure
.one("error")
Optional
Run 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
Reference
Cheat Sheet
Keep these facts handy while wiring image fallbacks.
Topic
Detail
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 tip
May not fire reliably under file: — use http(s)
Removed API
.error() removed in jQuery 3.0
Try it
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:).
Result
Ready — pick an action and click Run.
Code
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");
Swap.$(this).attr("src", fallback) replaces the broken URL.
Safe. A data: SVG cannot 404, so the handler does not loop.
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");
});
Broken avatar URL → error fires once
.src becomes inline SVG placeholder
.one() removed the handler → no loop
How It Works
Once..one("error") auto-unbinds after the first call.
Swap. The handler sets a data: SVG that always loads.
Safe. No second failure → no infinite error loop.
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).
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
errorUniversal
Bottom line: Safe for everyday apps. Bind before src, keep fallbacks valid, and never confuse this with window.onerror.
Analysis
Performance Notes
Approach
Cost
Best for
One .on("error") per image
Cheap
Small galleries / avatars
.one("error") + SVG fallback
Cheap
Loop-safe placeholders
Heavy work on every failure
Expensive
Avoid — 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.
Remember
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.