0

I am using JavaScript and jQuery to create a simple 40x40 grid.

Here's my nested for loop to do this:

function display_grid() {
  browser_grid = ''
  $visible_grid = $('#grid');

  for (i = 0; i < 40; i++) {
    $visible_grid.append('<div>');
    for (i = 0; i < 40; i++) {
      $visible_grid.append("<div class='square'> </div>");
    }
    $visible_grid.append('</div>');
  }
}

I expect this to create 40 divs each with 40 divs inside each one of them. The browser shows only one single row with 40 divs.

<div>
    <div class="square></div>
    <div class="square></div>
    <div class="square></div>
    ...
</div>

This is what I want it to do, but forty times. I'm not very experienced with JS, so I'm confused as to why the first loop isn't executing 40 times.

3
  • Note: The DOM API, that .append() depends upon, does not allow for injecting partial markup, such as '<div>' and '</div>' at separate times. It will forcibly "fix" the <div> into <div></div> and likely ignore the </div>. If you want to construct markup in parts, use a string and .append() the combined result. Commented Oct 2, 2015 at 23:44
  • I noticed that now that I fixed this. Should I just inject everything into a string then append it? Or is there a better way? Commented Oct 2, 2015 at 23:46
  • You are using the same global variable in both loops. Global variables should be avoided. Commented Oct 3, 2015 at 0:03

2 Answers 2

4

You need a different variable name for the inner loop.

function display_grid() {
    browser_grid='';
    $visible_grid = $('#grid');

    for(var i=0; i<40; i++){
        $visible_grid.append('<div>');
        for(var j=0; j<40; j++){
            $visible_grid.append("<div class='square'> </div>");
        }
        $visible_grid.append('</div>');
    }

Edit: Added code. Note that you should use the var keyword for counting variables in your for-loops.

What happened in your code is that after the 40 inner divs are created, the counter i is at 40 and the condition for the outer loop isn't true any longer, thus exiting that code block.

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for the help guys!

Changed the variable for my inner loop fixed the problem, but there was a problem with using .append()

Apparently it doesn't support partial markup, so instead I just appended all of my divs to a string instead.

for(var i=0;i<40;i++){
            browser_grid+='<div>';
            for(var x=0;x<40;x++){
                browser_grid+="<div class='square'> </div>";
            }
            browser_grid+='</div>';
        }

        $visible_grid.append(browser_grid)

After which, I appended the string to $visible_grid

This seemed to do what I wanted it to do. Just wanted to point it out for anyone else using the append method for this purpose.

1 Comment

Thanks for the reply. For info, maybe it's better written like this: browser_grid=''; onegrid=''; onegrid+='<div>'; for(var x=0;x<40;x++) { onegrid+='<div class="square"> </div>'; } onegrid+='</div>'; for(var i=0;i<40;i++) { browser_grid+=onegrid; } $visible_grid.append(browser_grid) Looks pretty similar but only use 2 loops with 40 iterations, instead of 40 loops with 40 iterations each, better perf I guess :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.