Trigger error event for the image
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>/* www . j ava 2 s . c om*/ <script> $(document).ready(function(){ $("img").error(function(){ $("img").replaceWith("<p>Error loading image!</p>"); }); $("button").click(function(){ $("img").error(); }); }); </script> </head> <body> <img src="image4.png" alt="image4.png" width="200" height="200"><br> <button>Test</button> </body> </html>