Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6210471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:09:53+00:00 2026-05-24T06:09:53+00:00

I have a for loop: for(var i = 0; i < r.length; i++){ var

  • 0

I have a for loop:

for(var i = 0; i < r.length; i++){
    var img=loadImage(r.id);
    $gallery.append(img);
}

and loading image function:

function loadImage(id){
    var $url='url/'+id;
    var img = new Image();
    $(img).load(function(){  
        return this;
    })     
    .attr({'src':$url,'id':id});
}

in the function above, I want to return the img so I can use it in the for loop, but this does not seem to work, I don’t get image to show up. I know I can use $gallery.append(this) inside load which works well, but I need to use the above method for other purposes. so any suggestion that I can do it in this way? thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:09:54+00:00Added an answer on May 24, 2026 at 6:09 am

    Using jquery’s load event to load images can be a bit buggy (taken from the jquery documentation) :

    • It doesn’t work consistently nor reliably cross-browser
    • It doesn’t fire correctly in WebKit if the image src is set to the same src as before
    • It doesn’t correctly bubble up the DOM tree
    • Can cease to fire for images that already live in the browser’s cache

    In your case, I’d use one of two different approaches:

    • callbacks
    • events

    With callbacks, you have the power to execute some functionality when an asynchronous task is done :

    function loadImage(id,callback){
        var $url='url/'+id;
        var img = new Image();
        $(img).load(function(){  
            if(typeof callback == 'function')
                callback(this);
             return this;
         }).attr({'src':$url,'id':id});
    }
    
    for(var i = 0; i < r.length; i++){
        var img=loadImage(r.id,function(_img){
             $gallery.append(_img);
        });
    
    }
    

    With events, you have even more power…
    You can create and trigger your own events and bind as many eventHandlers as you want.
    An example of using personal events:

    function loadImage(id){
        var $url='url/'+id;
        var img = new Image();
        $(img).load(function(){  
            $(document).trigger('my_special_event',[this]);
         }).attr({'src':$url,'id':id});
    }
    
    for(var i = 0; i < r.length; i++){
        var img=loadImage(r.id);
    }
    
    $(document).bind('my_special_event',function(e,img){
         $gallery.append(img);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a couple of arrays, like so: var galImgs = [http://domain.tld/gallery/image-001.jpg, http://domain.tld/gallery/image-002.jpg, http://domain.tld/gallery/image-003.jpg],
i have this scenario where im loading images using a for loop: for (var
i have an array like: var totalNum = new Array('img', 'img', 'img', 'img'); now
I have a loop created with each, check this example: $('.foo').each(function(i){ //do stuff });
I have this code: document.getElementById('img'+i).onclick = function(){popup_show('popup',array_msg[i]+'|||'+date('Y-m-d',strtotime(lec_date))+'-==-'+str_view_forConflict, 'AddEditSchedule;;popup_drag2;;EditSched;;'+String(array_msg_id[3])+';;view', 'popup_drag', 'popup_exit', 'screen-center', 0, 0);}; ...but
I have a for loop, and inside it a variable is assigned with var.
I have this code: <script type=text/javascript> var url = http://www.xxxxx.xxx/xxxxxxxxx; var txt; var id1;
If I have an array of image filenames, var preload = [a.gif, b.gif, c.gif];
Follow on from: Javascript wait for image to load before calling Ajax function initResources()
$(.IT_Badge).each(function(){ var badges = $(.IT_Badge).val().trim().split(,); for (c = 0; badges.length > c; c++) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.