 <p>You can try this <a href="http://jsfiddle.net/buhr164g/4/" rel="nofollow">http://jsfiddle.net/buhr164g/4/</a></p> <pre><code>$(function(){ var p ='http://localhost.com/sampleimage'; for(var i=0;i&lt;10;i++) { $("body").append('&lt;img class="profile-pic" src="'+p+'_'+i+'.jpg"/&gt;'); } $(".profile-pic").error(function(){ /* Do error processing */ }).attr('src',"http://journal.appconnect.in/wp-content/uploads/2013/08/runner_small.gif"); }); </code></pre>