jQuery error() handle error event

Description

jQuery error() handle error event

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>/*from   ww w.j  a  va 2 s .  c om*/
<script>
$(document).ready(function(){
  $("img").error(function(){
    $("img").replaceWith("<p>Error loading image!</p>");
  });
});
</script>
</head>
<body>

<img src="image4wrongName.png">

<p>If the image above encounters an error, it will be replaced with a specified text.</p>

</body>
</html>



PreviousNext

Related