Skip to content

Instantly share code, notes, and snippets.

@rmw
Created June 18, 2012 19:49
Show Gist options
  • Save rmw/2950332 to your computer and use it in GitHub Desktop.
Save rmw/2950332 to your computer and use it in GitHub Desktop.
Showing a default image if an image doesn't exist in jQuery
var img = $('img');
var default_url = "http://example.org/images/img.jpg";
var img_url = "http://example.org/images/" + some_variable + ".jpg";
img.error(function() {
$(this).attr('src', default_url)
});
img.attr('src', img_url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment