In the closure you have to set the arguments at the start too:

return function(parentDiv,image) {
...
}(parentDiv,image);

but I think it won't work this way because your closure won't be
executed right away (as it is inside an event handler) so it doesn't
make much sense to put it there.

Start the closure right after the loop and you don't have to care
about it any more:

for(i=0; i<imgs.length; i++) (function(i){
...
})(i)


But this is still unnecessary because you can just use $.each :)

$.each(imgs, function(i,e){
...
})

On Dec 29, 2:07 pm, Joel <[email protected]> wrote:
> JQuery users,
>
> I'm receiving the following error:
>
> Error: [Exception... "Could not convert JavaScript argument arg 0
> [nsIDOMViewCSS.getComputedStyle]"  nsresult: "0x80570009
> (NS_ERROR_XPC_BAD_CONVERT_JS)"  location: "JS frame 
> ::http://REMOVED.net/jquery-1.2.6.min.js:: anonymous :: line 22"  data:
> no]
> Source File:http://REMOVED.net/jquery-1.2.6.min.js
> Line: 22
>
> When I run this code:
>
> $(function () {
>     var imgs = $("div").filter(".fc_Picture");
>     var parentDiv;
>     for(i=0; i<imgs.length; i++) {
>         var img = new Image();
>         parentDiv = imgs[i];
>         $(img)
>             .load(function(parentDiv,image) {
>                 return function() {
>                     $(image).hide()
>                     $(parentDiv).removeClass('fc_loader');
>                     $(parentDiv).append(image);
>                     $(image).fadeIn();
>                 }(parentDiv,image);
>             })
>             .attr('src','fc_image.php?imageid='+$(imgs[i]).attr
> ('id').split("_")[2])
>             .attr('width','50')
>     }
>
> });
>
> What I'm trying to do is have a set of images and each one loads with
> one of those cute little loader swirl things. I understand the
> pitfalls of for loops and closures (after much head scratching) but I
> still can't get it to work and to be honest I haven't the faintest
> what that error message means.
>
> Thanks for your help
>
> Joel

Reply via email to