error() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:error

Description

The error() method was deprecated in jQuery version 1.8, and removed in version 3.0.

The error() method triggers the error event, or sets a function as error event handler.

Syntax

$(selector).error(function);
Parameter Description
function Optional. Specifies the function to run when the error event occurs

The following code shows how to trigger the error event for the selected elements:

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

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("img").error(function(){
        $("img").replaceWith("<p>Error loading image!</p>");
    });//www . ja v  a  2s.c  o  m
    $("button").click(function(){
        $("img").error();
    });
});
</script>
</head>
<body>

<img src="http://java2s.com/resources/a.png" alt="Pulpit rock" width="284" height="213"><br>

<button>Trigger error event for the image</button>

</body>
</html>

Related Tutorials