I am trying to attach load
and error
event to some images which are loaded dynamically. Here is the code:
$(document).on('load', '.no-image-aware', function(){
$(this).on('error', function(){
$(this).attr('src','../resources/images/no-image.jpg');
});
});
This is not working. Even:
$(document).on('load', '.no-image-aware', function(){
console.log($(this));
});
is not printing anything in console.
But if I do this:
$('.no-image-aware').load(function(){}).error(function(){
$(this).attr('src','../resources/images/no-image.jpg');
});
It works. But it works only when the images with style class no-image-aware
are present at the time of the component creation.
I am using ADF Faces. In ADF Faces some component loads their children later. So I need to attach the event handler to these images as descendants of the parent component. But this is not likely ADF Faces issue. What I want is to add load
event for future images.
Isn't it possible to attach load
event to the images? For click
or mouseover
it works. So why not for load
?
I am using jQuery 1.10.0. Any pointer would be very helpful to me.
Here is JSFiddle which emulates the stuff what actually I want to achieve.