Search code examples
javascriptappendchild

isappendChild() a function for an anodeList


const cardSet = ['fa-diamond','fa-diamond','fa-paper-plane-o','fa-paper- 
plane-o','fa-anchor','fa-anchor','fa-bolt','fa-bolt','fa-cube','fa-cube','fa- 
leaf','fa-leaf','fa-bicycle','fa-bicycle','fa-bomb','fa-bomb'];
let cardsOpen=[];
let cardsflipped = 0;
let theDeck= document.getElementsByClassName('deck');


 window.onload=function startGame(){
shuffle(cardSet);
let cards;
cardSet.forEach(function (){
    cards= document.createElement('li');
    cards.classList.add('card')
    cards.innerHTML='<i class="fa '+ cardSet +'"></i>';
});
theDeck.appendChild(cards);
};

Hello! I have been trying to create a memory game. This function initiate the game. But everytime I run the code an error occurs saying appendChild(cards) is not a function. can someone help?


Solution

  • let theDeck= document.getElementsByClassName('deck');

    creates a node list and .appendChild() is not a method of node lists, it's a method of a single node. You'll need to reference a single, specific node and call .appendChild() on that.