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 8283781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:51:49+00:00 2026-06-08T10:51:49+00:00

I seem completely unable to solve this problem. When I run this loop: for

  • 0

I seem completely unable to solve this problem. When I run this loop:

for ( var i=0; i < 10; i++ ) {
    var $items = $(balls()); 
    console.log($items);
    $container.imagesLoaded(function(){
        $container.append( $items ).masonry( 'appended', $items, true );
    });
}

The first item, and the last item are fine, they look like:

<div class="box masonry-brick" id="resultBox" style="top: 0px; position: absolute; left: 0px; "><div><p>NAME</p><img src="pathtoimage.jpg"><boxbottom id="boxBottom">NUM%</boxbottom></div></div>

But the other 8 aren’t fine, they look like:

<div class="box" id="resultBox""><div><p>NAME</p><img src="pathtoimage.jpg"><boxbottom id="boxBottom">NUM%</boxbottom></div></div>

So there’s no ‘masonry-brick’ class being applied, which means they aren’t getting processed by masonry.

Also relevant is:

function balls(){
    var boxes = [];
    $iterator -= 1;
    var box = document.createElement('div'),
        spacerdiv = document.createElement('div'),
        para = document.createElement('p'),
        img = document.createElement('img'),
        boxBottom = document.createElement('boxBottom'),
        name = document.createTextNode( $test[$iterator][1][2]['name'] ),
        percentage = document.createTextNode(Math.floor($test[$iterator][0]*100)+'%');

    box.className = 'box';
    box.id = 'resultBox';
    img.src= 'scripts/php/timthumb.php?src='+$test[$iterator][2]+'&q=100&w=300';
    boxBottom.id='boxBottom';

    box.appendChild( spacerdiv );
    spacerdiv.appendChild( para);
    para.appendChild(name);
    spacerdiv.appendChild( img );
    spacerdiv.appendChild(boxBottom);
    boxBottom.appendChild(percentage);
    // add box DOM node to array of new elements
    return box;
}

I think there’s something odd going on with the DOM model here, perhaps because the elements are being created dynamically?
I’ve been at this all day, and I’m getting burned out…

  • 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-06-08T10:51:50+00:00Added an answer on June 8, 2026 at 10:51 am

    I assume each invocation of .imagesLoaded() is binding your callback function to be run later, asynchronously, after images are in fact loaded. If so, you could change your code to something like this:

    for ( var i=0; i < 10; i++ ) {
       (function() {
           var $items = $(balls()); 
           console.log($items);
           $container.imagesLoaded(function(){
               $container.append( $items ).masonry( 'appended', $items, true );
           });
       })();
    }
    

    …because JS doesn’t have block scope, it has only function scope. So even though you placed the var statement inside the loop your $items variable isn’t actually limited in scope to the loop and when the callbacks setup with .imagesLoaded() are executed they all reference the same $items – which will hold the value as at the end of the loop. By wrapping your code in a function (in this case an anonymous function expression that is immediately executed – note the () on the end) a new $items variable is created for every iteration of the loop and thus each of your .imagesLoaded() callbacks references the correct $items.

    More efficient version (because it doesn’t create a new function on each iteration):

    function addBalls() {
       var $items = $(balls()); 
       console.log($items);
       $container.imagesLoaded(function(){
           $container.append( $items ).masonry( 'appended', $items, true );
       });
    }
    for ( var i=0; i < 10; i++ )
        addBalls();
    

    Either way, if you actually need to reference the loop index i within the loop you’d just pass it as a parameter to the function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seem to run into a service endpoint not found problem when trying to get
I delved into C# but came across a problem that I seem unable to
I seem to be completely unable to change the font-size of the jQtransform dropdowns.
This is very stupid but I seem to be completely lost trying to test
I'm using NetBeans for a project, and baffled that I seem completely unable to
This question may seem completely stupid, but say i have a PHP page with
I can't seem to figure out why SQL Server is taking a completely different
I seem to be a little lost on what to so about this, im
I seem to have a problem passing some strings on from one form to
I can't seem to get this little AIR app I'm making to work right.

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.