How to use jQuery event error() Method

This article describe about jQuery Event error() Method.
  • 2428

jQuery Event error() Method

If the element is not loaded correctly then error event occurs and it encounter an error.

The error event trigger with error() method or specifies a function to run when an error event occurs.

Note: Shortcut of this method is (bind , handler).

Example

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

    $(document).ready(function () {

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

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

        });

        $("button").click(function () {

            $("img").error();

        });

    });

</script>

</head>

<body>

<img src="5722750107_21488e5a51_b1.jpg" alt="kas3h.jpg" width="284" height="213" />

<br />

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

</body>

</html>

 

Output


error method pic14.jpg

 

Note: After click on the button output become.


error method pic15.jpg

 

Trigger the error event

Generate the trigger with error event for selected element.

 

Syntax

 

$(selector).error()

 

Bind a Function to the error Event

 

It is define the function that run when the error event occur for the specific selected element.

 

Syntax

 

$(selector).error(function)

 

You may also want to read these related articles Click here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.