2

Can you please help how to append Divs side by side using javascript using loops, here two columns and 6 rows. I tried the following code,

for(var i=0;i<2;i++) {
    for(var j=0;j<5;j++) {
        document.write('<div style="width:200px;height=200px;border:1px solid red;float:left" />')+"\n";    
    }
}
6
  • 5
    Isn't this moar of a CSS issue rather than a JS issue? Also please don't use document.write(). Commented Aug 29, 2012 at 14:37
  • 2
    it works - jsfiddle.net/h3NMn Commented Aug 29, 2012 at 14:38
  • Maybe you should be using a table? Commented Aug 29, 2012 at 14:38
  • That would produce two rows and four columns, but the way it is written [minus the \n error] it would be one row until it wraps in the browser. Commented Aug 29, 2012 at 14:39
  • @ZoltanToth try to change resolution .. Commented Aug 29, 2012 at 14:41

5 Answers 5

2

html:

​<div id="table"></div>​

js:

var newdiv;
for(var i=0;i<2;i++) {
    for(var j=0;j<6;j++) {
        newdiv=document.createElement("div");
        newdiv.style.width = '80px';
        newdiv.style.height = '80px';
        newdiv.style.border = '1px solid red';
        newdiv.style.float = 'left';
        document.getElementById('table').appendChild(newdiv);    
    }
    newdiv=document.createElement("div");
    newdiv.style.clear = 'both';
    document.getElementById('table').appendChild(newdiv);
}​
Sign up to request clarification or add additional context in comments.

Comments

0

I think you have added the "\n" at wrong place.

 document.write('<div style="width:200px;height=200px;border:1px solid red;float:left" />'+"\n");

Tested code:-

function CreateDiv()
{
for(var i=0;i<2;i++) {
        for(var j=0;j<5;j++) {
            document.write('<div style="width:200px;height=200px;border:1px solid red;float:left" >Hi</div>');  
        }
    }
}

Comments

0

It seems working, take a look: http://jsfiddle.net/fCSTp/1/

var i = 0, div,
    node = document.createElement("div"),
    main = document.getElementById("main");

while (6 > i++) {
    div = node.cloneNode(true);
    div.innerHTML = i;
    div.style.cssText = "width:200px;height:20px;float:left;border:1px solid red";
    main.appendChild(div);
}

delete node;
delete div;

Comments

0

Try this

http://jsfiddle.net/DsqMj/4/

HTML

<div id="table"></div>

CSS

#table .box {
width: 100px;
height: 30px;
background-color: #ccc;
}
#table .box:nth-child(even) {
background-color: #eee;
}
#table .col {
width:100px;
border: 1px solid #aaa;
float: left;
}

JS

makeTable();

function makeTable() {
var table = document.getElementById("table");
var row ='';

for(var i=0;i<2;i++) 
{    
    for(var j=0;j<6;j++) 
    {
        row += "<div class='box'></div>";
    }
    table.innerHTML += "<div class='col'>"+row+"</div>";
    row ='';
}
}

Comments

-1

Insert a <br style="clear:both"/> at the end of each row:

for(var i=0;i<2;i++) {
    for(var j=0;j<5;j++) {
        document.write('<div style="width:200px;height=200px;border:1px solid red;float:left" />')+"\n";    
    }
    document.write('<br style="clear:both"/>');
}

Comments

Your Answer

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