tags:

views:

77

answers:

5

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.

+3  A: 

When you view the code in the browser, you see the original code sent by the server. You should see an instance of the DOM to verify that the class is there. You can do that easily in Firefox with Firebug.

Unless you have reloaded the page, the new query shouldn't match the images that the imageloading class was removed. Check also that the code that runs the removeClass method operates as expected. Firebug is a great tool to test such behaviour.

kgiannakakis
That's right, you'll see the original code without manipulation. Some browsers even do the http request again to fetch the source code. As suggested, use Firefox with the firebug extension to see what's happening to the DOM on runtime. http://getfirebug.com/
Tim Büthe
OK, even when I don't see the changes, probably I'd have never seen, but the problem is that after the class removing nevertheless all images are returned.
centro
Debug the script with Firebug to solve the problem yourself or post full source here to get some help.
kgiannakakis
I'm still trying to solve the problem by debugging. Also, I updated the post by adding more details on source.
centro
+1  A: 

Is that behavior by design that all visual changes are applied but the class attribute is not removed in HTML code?

answer: YES

with firebug in firefox, or web dev tools in google chrome, you can see the changes... but not the original code changes...

Reigel
+3  A: 

try .removeClass('imageLoading'); in place of .removeClass('imageloading');

Samuel
what??????????? WTF +1 but this one is not the issue... ;)
Reigel
css is case sensitive
Samuel
That's not the point, it's just a mistype. I'll edit it.
centro
+3  A: 

Inspect using a debugging utility like firebug and observe the result. Changes that you make to the DOM using javascript won't be reflected in page source.

rahul
A: 

I'm sorry about much ado about nothing. I've just incorrectly passed the jQuery object into the subroutine:

removeImageLoadingClasses(this)

instead of

removeImageLoadingClasses($(this))

So it's okay now.

centro