I have images on my page. User can add more images onto the page by clicking a button. New images are added asynchronously. Initially, each image on page use a special class to be used when the image is loaded. After the image is loaded, that class is removed. Each image being loaded has the class imageLoading:
<img class="imageLoading" scr="someimage.png">
After those images are loaded, I remove that class (simplified code without details):
$('img.imageLoading')
.each(function(){ $(this)
.load(function(){ $(this)
.removeClass('imageLoading');
});});
Visually, I see that style is removed. But when I run the query again:
$('img.imageLoading')
I see via debugging that all images, not just loading ones, are returned, i.e. it works like I didn't remove the class for the images that were already loaded. I had a look into the page source, and I saw that actually in HTML the class was not removed, though removeClass() was called.
Is that behavior by design that all visual changes are applied but the class attribute is not removed in HTML code? If so, how it can be workarounded in this case. Or, probably, I missed something.
UPD Source code
I didn't resolved the problem yet, so post some code to show how it works and where the problem is. My code works this way (unimportant details removed):
Loading images are processed:
after page loading:
<script type="text/javascript">
$(function() { processLoadingImages(); })
</script>
after asynchronous call:
function onRequestComplete(result) {
var div = document.createElement('div');
div.innerHTML = result;
document.getElementById('images').appendChild(div);
processLoadingImages();
}
Processing images by processLoadingImages function:
function processLoadingImages() {
$('img.loadingImage').each(function() {
if (!imageLoaded($(this)[0])) {
$(this).hide()
.load(function() {
removeImageLoadingClasses(this);
$(this).fadeIn();
});
}
else {
removeImageLoadingClasses(this);
}
});
}
function removeImageLoadingClasses(jqueryObj) {
jqueryObj.removeClass('loadingImage');
jqueryObj.parents('div').removeClass('loadingImageContainer');
}
function imageLoaded(img) {
if (!img.complete)
return false;
if (typeof img.naturalWidth != 'undefined' && img.naturalWidth == 0)
return false;
return true;
}
As you can see, for each image there is a div container with a class that is removed for loaded images. How did I check that the query retuns all images even those for which removeClass was called? I debugged imageLoaded function (see above), and it's always called for all the images.