Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple divs with appendChild?

I am trying to make a chessboard using javascript and creating 64 divs with it.
The problem is, that it creates only the first div.
Here is the code:

div {     width: 50px;     height: 50px;      display: block;     position: relative;     float: left; }  <script type="text/javascript">     window.onload=function()     {         var i=0;         var j=0;         var d=document.createElement("div");          for (i=1; i<=8; i++)         {             for (j=1; j<=8; j++)             {                 if ((i%2!=0 && j%2==0)||(i%2==0 && j%2!=0))                 {                     document.body.appendChild(d);                     d.className="black";                 }                 else                 {                     document.body.appendChild(d);                     d.className="white";                 }             }         }     } </script> 
like image 972
svtslvskl Avatar asked Feb 16 '13 12:02

svtslvskl


People also ask

Can I appendChild multiple elements?

To append multiple child elements with JavaScript, we can use the append method. Then we write: const listElement = document.

Can you append multiple items at once JavaScript?

append() The append method allows you to append multiple elements or text nodes to a parent node. As you can see, you can supply an unlimited amount of arguments to the append() method and it will append everything to the parent.

Should I use append or appendChild?

Difference between appendChild() and append()append() also allows you to append DOMString objects, and it has no return value. Further, parentNode. appendchild() allows you to append only one node, while parentNode. append() supports multiple arguments - so you can append several nodes and strings.


2 Answers

As t-j-crowder has noted, the OP's code only creates one div. But, for googlers, there is one way to append multiple elements with a single appendChild in the DOM: by creating a documentFragment.

function createDiv(text) {   var div = document.createElement("div");   div.appendChild(document.createTextNode(text));   return div; }  var divs = [   createDiv("foo"),   createDiv("bar"),   createDiv("baz") ];  var docFrag = document.createDocumentFragment(); for(var i = 0; i < divs.length; i++) {   docFrag.appendChild(divs[i]); // Note that this does NOT go to the DOM }  document.body.appendChild(docFrag); // Appends all divs at once 
like image 135
Renato Zannon Avatar answered Sep 20 '22 15:09

Renato Zannon


The problem is, that it creates only the first div.

Right, because you've only created one div. If you want to create more than one, you must call createElement more than once. Move your

d=document.createElement("div"); 

line into the j loop.

If you call appendChild passing in an element that's already in the DOM, it's moved, not copied.

window.onload=function()     {         var i=0;         var j=0;          for (i=1; i<=8; i++)         {             for (j=1; j<=8; j++)             {                 if ((i%2!=0 && j%2==0)||(i%2==0 && j%2!=0))                 {                     var d=document.createElement("div");                     document.body.appendChild(d);                     d.className="black";                 }                 else                 {                     var d=document.createElement("div");                     document.body.appendChild(d);                     d.className="white";                 }             }         }     } 
like image 27
T.J. Crowder Avatar answered Sep 21 '22 15:09

T.J. Crowder