Showing a default image if one doesn’t exist in jQuery

Posted on June 27, 2012

Have you ever wanted to default an image if the image url doesn’t exist? In javascript, how do you check whether that url is real or not? Here’s how! (in jQuery)

Turns out you can attach an error handler to a jQuery element. This allows you to do something (i.e. set the src attribute to the default image) if the loading on the image fails. It’s pretty sweet.


No Replies to "Showing a default image if one doesn't exist in jQuery"


    Leave a Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.