简体   繁体   中英

Creating a dynamic grid with Javascript

I'm very new to JS so pardon anything that may be totally, utterly wrong.

I'm trying to create a 16x16 grid of divs dynamically. My logic is that I'd create a container for all of the grid, inside the container I'd append 16 rows and in each row I'd append 16 boxes. I have a rough idea of my code and I wanted to check if it is valid logic and JS.

/* Creating the grid */
function grid() {
    var container = document.createElement("div");
    container.id = "main";
    container.className = "container";

    for (i=0, i<16, i+=1) {
        var row = document.getElementbyId('main').appendChild('div');
        row.className = "row";
        row.id = "row" + i;
    };

    for (j=0, j<16, j+=1) {
        for (k=0, k<16, k+=1) {
            var box = document.getElementbyId("row" + j).appendChild('div');
            box.className = "box";
        };
    };
};

CAUSE

There are some issues with the code.

  • Syntax for for loop is for(init;condition;final-expression) , see manual for for .
  • appendChild requires nodes not strings.
  • Function grid() doesn't do anything. It should either return a node, accept a node to append to or insert content somewhere, it's up for you to decide.

DEMO

See the demo below for corrected code and demonstration.

 /* Creating the grid */ function grid(el) { var container = document.createElement("div"); container.id = "main"; container.className = "container"; for (i=0; i<16; i+=1) { var row = document.createElement("div"); row.className = "row"; row.id = "row" + i; for (k=0; k<16; k+=1) { var box = document.createElement("div"); box.className = "box"; row.appendChild(box); }; container.appendChild(row); }; el.appendChild(container); }; grid(document.body); 
 .row { border:1px solid green; height:1em; line-height:1em; } .box { display:inline-block; width:6.25%; height:1em; box-sizing:border-box; border:1px solid red; } 

Yep the logic is correct although theres a number of syntax/type errors and it would be more efficient too store reusable variables and to have only 2 loops as below

function grid(d) {
 var container = d.createElement("div");
 container.id = "main";
 container.className = "container";

  for (i=0; i<16; i++) {
    var row = container.appendChild(d.createElement('div'));
    row.className = "row";
    row.id = "row" + i;

     for (k=0; k<16; k++) {
        var box = row.appendChild(d.createElement('div'));
        box.className = "box";
     };
  };

  d.body.appendChild(container);

};

grid(document);

 /* Creating the grid */ function grid (content) { var container = content.appendChild(document.createElement("div")); container.id = "main"; container.className = "container"; for (var i = 0;i < 16;++i) { var row = container.appendChild(document.createElement("div")); row.className = "row"; row.id = "row" + i; for (var k = 0;k < 16;++k) { var box = row.appendChild(document.createElement("div")); box.className = "box"; } } } grid(document.body) 

Some errors in your code:

  • container needs to be inserted somewhere too.
  • appendChild accepts nodes, not strings
  • for loop uses ; to separate expressions, not , (they all are optional)

You should pay attention to your misuse of syntax in for loop.

You may want this:

'use strict';

function grid() {
    var container = document.createElement('div');
    container.id = "main";
    container.className = "container";

    for (var i = 0; i < 16; i++) {

        var row = document.createElement('div');
        row.className = "row";
        row.id = "row" + i;

        for (var j = 0; j < 16; j++) {
            var box = document.createElement('div');
            box.className = 'box';
            row.appendChild(box);
        }

        container.appendChild(row);
    }

    return container;
}

console.log(grid());

// and you should use 
// document.getElementById('xxx').appendChild(grid());

You may refer to:

You have so many syntax errors that you need to be aware of, like :

The for loop, it should contain semi colons not commas...

And you need to create the div everytime before appending it, like you did for the container.

Here's a working code :

function grid(){
    var container = document.createElement("div");
    container.id = "main";
    container.className = "container";
    document.body.appendChild(container);
    var main = document.getElementById('main');
    for (var i=0; i<16; i++) {
        var row = document.createElement("div");
        row.className = "row";
        row.id = "row" + i;
        main.appendChild(row);
        var roww = document.getElementById('row'+i);
        for (var j=0; j<16; j++) {
            var box = document.createElement("div");
            box.className = "box";
            roww.appendChild(box);
        }
    }
}
window.onload = grid();

Here's A fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM