I am creating some images in my page. When I write <img src=somesrc, onerror=othersrc></img>
, it works fine.
But when I create image using jQuery it fails:
jQuery('<img />', {
onerror: "javascript: this.src='some_default_image_source'",
src: 'image_source'}).appendTo("#container");
How do I solve this?
I am creating some images in my page. When I write <img src=somesrc, onerror=othersrc></img>
, it works fine.
But when I create image using jQuery it fails:
jQuery('<img />', {
onerror: "javascript: this.src='some_default_image_source'",
src: 'image_source'}).appendTo("#container");
How do I solve this?
Share Improve this question asked Jun 18, 2012 at 1:09 onemachonemach 4,3257 gold badges36 silver badges55 bronze badges 2- 1 But when I create image using jQuery it fails How does it fail? What exactly fails? – Felix Kling Commented Jun 18, 2012 at 1:33
- @FelixKling the onerror code does not get executed and so the default image does not get loaded. – onemach Commented Jun 18, 2012 at 3:34
2 Answers
Reset to default 7jQuery('<img />').error(function() {
this.src = 'some_default_image_source';
}).attr('src', 'image_source').appendTo("#container");
Works fine. Link to jsFiddle plus extra words