0

I am trying to create a grid like structure. I want to create a loop within a loop and append divs based on if it is a column or a row. Below is my attempt:

for (var row = 0; row < 8; row++) {
    $('#board').append('<div class="row">'+ row +'</div>');  
}

What I would like to replicate in plain Javascript (code is most likely incorrect, I just want to show an example of what I want):

var board = document.getElementById('board');

for (var row = 0; row < 8; row++) {
    board.write('<div class="row">');
    for(var col = 0; col < 8; col++) {
        var row = document.getElementsByClassName('row');
        row.write('<div class="piece">'+col+'</div>')
    }
    board.write('</div>');
}

It would be useful if I could somehow replicate the document.write() method in jQuery. .append() did not work correctly for me when I tried to include a forloop.

8
  • 2
    Instead, simply generate a long html string, then append the string in the end. You won't have to change much of your logic. (you have a var name conflict with row that will cause you issues.) Commented Aug 19, 2013 at 15:57
  • document.write() is like setting the .innerHTML property of an element (could be document.body). jQuery's .append() does a lot better job of parsing the string and adding it to the DOM Commented Aug 19, 2013 at 15:57
  • you need to look at appendChild Commented Aug 19, 2013 at 15:58
  • @Ian, I tried .innerHTML but it does not concatenate properly. When I write: board.innerHTML = '<div class="row">'; This automatically closes the div. Commented Aug 19, 2013 at 16:00
  • 1
    @Shivam Not exactly. That is only true on a document that is still open, such as the DOM before it is ready while it is still being parsed. developer.mozilla.org/en-US/docs/Web/API/document.write Commented Aug 19, 2013 at 16:03

3 Answers 3

2

You just need to loop through rows and then cells to build a single html string and than append it only once:

var rows = [], cells = [];
for (var row = 0; row < 8; row++) {
    cells = [];
    for (var cell = 0; cell < 8; cell++) {
        cells.push('<div class="piece">'+ row +'</div>');
    }
    rows.push('<div class="row">' + cells.join('') + '</div>');
}

$('#board').html(rows.join(''));

DEMO HERE

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

Comments

1

What I understand you want to end up with a structure like this:

<div id="board">
  <div class="row">
    <div class="piece"></div>
    <div class="piece"></div>
    <div class="piece"></div>
    (...)
  </div>
  (...)
</div>

Let's assume you already have a board (<div id="board"></div>).

for( var row = 0; row < 8; row++ ) {
  //Create a row
  $row = $('<div class="row"></div>');

  //Stuff 8 pieces in that row
  for( var col = 0; col < 8; col++ ) {
    $row.append( $('<div class="piece">' + col + '</div>') );
  }

  //Add the row to the board
  $('#board').append( $row );
}

Edit: In your case you can even simplify it to:

//Stuff 8 rows in the board
for( var row = 0; row < 8; row++ ) {
  $('#board').append( $('<div class="row"></div>') );
}

//Stuff 8 pieces in every of the 8 rows
//This can be done because the piece is the same for every row
for( var col = 0; col < 8; col++ ) {
  $('.row').append( $('<div class="piece">' + col + '</div>') );
}

jsFiddle: http://jsfiddle.net/3jGnF/

1 Comment

this is very inefficient way of building HTML, as you're changing the dom with every step of the loop. Instead - you should build a html string and then - at the end - append it to the dom (only once).
1

Try the following - if you need to manipulate DOM more specifically, then use JQuery

var board = document.getElementById('board'),
    html = '';

for (var row = 0; row < 8; row++) {
    html += '<div class="row">';
    for(var col = 0; col < 8; col++) {
        html += '<div class="piece">'+col+'</div>';
    }
    html += '</div>';
}

board.innerHTML = html;

Or with JQuery:

var board = $('#board');
...
board.append($(html));

1 Comment

Ok thanks to uncommented downvote... there was 1 : instead of ;, hard to see on this display.

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.