Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`appendChild` inside a `for` loop just replaces item created by `createElement`

I Googled a lot about creating multiple items with appendChild, but I’m not understanding how it works. My appendChild just replaces instead of adding many.

var startGame;
var cards = 16;
var newDeck = [];

startGame = function(){
    var startBtn = document.getElementById('start');
    var board = document.getElementById('game-board');
    var backside = document.createElement("div");
    backside.className = 'card';

    startBtn.onclick = function(){
        removeButton = document.getElementById("start");
        removeButton.parentNode.removeChild(removeButton);

        for(var i = 0; i < cards; i++){ 
            board.appendChild(backside);
        }
    };
};

I also read you can do this with innerHTML, but that leaves me confused as well. Does anyone have a more detailed explanation on how to make this work?

like image 596
Cammy Avatar asked Mar 26 '13 17:03

Cammy


People also ask

What does createElement () do?

In an HTML document, the document.createElement() method creates the HTML element specified by tagName, or an HTMLUnknownElement if tagName isn't recognized.

What does document createElement (' a ') return?

The nodeName of the created element is initialized to the elementName value. The document. createElement() returns the newly created element. Example 1: This example illustrates how to create a <p> element.

Where is a new element created using the createElement () method added?

createElement() method to create a new button element. Here, we are using the insertBefore() method to insert the created element. There is a div element that has a paragraph child element. The new button element will be inserted before the paragraph child element of the div element.

Does the document createElement () method insert a new element into the page?

The document. createElement() creates a new HTML element. The element. appendChild() appends an HTML element to an existing element.


2 Answers

From the MDN on appendChild :

Adds a node to the end of the list of children of a specified parent node. If the node already exists it is removed from current parent node, then added to new parent node.

When you append an element that is yet in the DOM, you move it from its old place. Create the element in the loop :

startBtn.onclick = function(){
    removeButton = document.getElementById("start");
    removeButton.parentNode.removeChild(removeButton);

    for(var i = 0; i < cards; i++){ 
        var backside = document.createElement("div");
        backside.className = 'card';
        board.appendChild(backside);
    }
};
like image 136
Denys Séguret Avatar answered Sep 19 '22 07:09

Denys Séguret


You're creating a single element and trying to re-add it multiple times. You need to create multiple elements.

When you run document.createElement that creates the element in the DOM. AppendChild is just setting the location. So you create one element and then move it to the same place many times. You want to instead create many elements and set their location once each

var backside;
startBtn.onclick = function(){
    removeButton = document.getElementById("start");
    removeButton.parentNode.removeChild(removeButton);

    for(var i = 0; i < cards; i++){ 
        backside = document.createElement("div");
        backside.className = 'card';
        board.appendChild(backside);
    }

or alternatively (shorter but less flexible, only use this for a one-off)

startBtn.onclick = function(){
    removeButton = document.getElementById("start");
    removeButton.parentNode.removeChild(removeButton);

    for(var i = 0; i < cards; i++){ 
        board.appendChild("<div class='card'></div>);
    }
like image 24
Ben McCormick Avatar answered Sep 20 '22 07:09

Ben McCormick