jQuery error() method
The error() method is used to trigger an event when an element encounters an error. It was deprecated in jQuery version 1.8 and removed in version 3.0. We can use the jQuery version 2.2.1 in order to use the error() method.
The error handler in jQuery should not be attached to the window object. Generally, the error occurs when the element is not loaded properly. The error() method is a shorthand for bind(‘error’,handler).
Syntax
We can either simply use the error() method or can add a function in the error event. The syntax of using the error() method is given as follows –
Trigger the event for selected elements
Attach a function
The parameter function mentioned in the above syntax is optional. It is a function to run when the error event is triggered.
Example
It is a simple example of using the error() method. In this example, if the given image element encounters the error, it will be replaced with a text.
Output
After the execution of the above code, the output will be –
After clicking the given button, the image will be replaced with a text as shown in the below screenshot –