jQuery Event - error() Method

Example

If the image does not exist, replace it with a predefined text:

$("img").error(function(){
  $("img").replaceWith("

Missing image!

"); });

Try it yourself

Definition and Usage

An error event occurs when an element encounters an error (not loaded correctly).

The error() method triggers an error event or specifies a function to be executed when an error event occurs.

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

Triggers an error event

Syntax

$(selector).error()

Try it yourself

Binds a function to the error event

Syntax

$(selector).error(function)
Parameter Description
function Optional. Specifies a function to be executed when an error event occurs.

Try it yourself