Example

If the image element encounters an error, replace it with a predefined text:

$("img").error(function(){
  $("img").replaceWith("<p>Error loading image!</p>");
});

Try it yourself »

Definition and Usage

The error event occurs when an element encounters an error (if the element is not loaded correctly).

The error() method triggers the error event, or specifies a function to run when an error event occurs.

Tip: This method is a shortcut for bind('error', handler).


Trigger the error Event

Trigger the error event for the selected elements.

Syntax

$(selector).error()
Try it yourself »


Bind a Function to the error Event

Specifies a function to run when the error event occurs for the selected element.

Syntax

$(selector).error(function)
Try it yourself »

Parameter Description
function Optional. Specifies the function to run when the error event occurs