Home > database >  How to clear the innerHTML content of an HTMLCollection?
How to clear the innerHTML content of an HTMLCollection?

Time:11-27

I am building a Tic Tac Toe game. My problem is being unable to clear the innerHTML of multiple divs (div class .cell) when clicking the resetButton. The divs I am trying to clear are the children of gameDisplay.

When I tried to locate the cell elements in my chrome dev tool, I get an HTML Collection: ScreenShot Here

I am having difficulty accessing the innerHTML of these elements. I currently have my resetGame() function returning gameDisplay.innerHTML=""(line 79) however, that removes all of the content of the parent element and removes my grid(board) completely. I know that is not specific enough. As a result, I tried returning gameDisplay.children.innerHTML =""; instead but nothing happens in the DOM or in my chrome dev tools console.

How can I access the innerHTML of these divs and remove the HTML inside of them using the resetGame function?

JS

const playerFactory = () => {
    const playTurn = (event, currentPlayer) => {
      const id = boardObject.cells.indexOf(event.target);
      boardObject.boardDisplay[id] = currentPlayer;
      boardObject.render();
    };
  
    return {
      playTurn
    };
  };
  
  // Gameboard object
  const boardObject = (() => {
    let boardDisplay = ['', '', '', '', '', '', '', '', ''];
    const cells = Array.from(document.querySelectorAll(".cell"));
    // displays the content of the boardArray
    const render = () => {
      boardDisplay.forEach((cells, idx) => {
        cells[idx].innerHTML = boardDisplay[idx];
      });
    };
    return {
      boardDisplay,
      render,
      cells
    };
  })();


  // Create Array from DOM
const boardArray = [...document.querySelectorAll(".cell")].map(cells=>cells.innerHTML);
console.log(boardArray);

// Display controller, shows which player is which
const displayController = (() => {
const playerOne = 'X';
const playerTwo = 'O';
const gameBoard = document.querySelector(".game-board");
let currentPlayer = playerOne;

const switchPlayer = () => {
    currentPlayer = currentPlayer === playerOne ? playerTwo : playerOne;
  }

  gameBoard.addEventListener("click", (event) => {
    if (event.target.classList.contains("cell")) {
      if (event.target.textContent === "") {
        event.target.textContent = currentPlayer;
        const indexOfClickedCell = Array.prototype.indexOf.call(document.querySelectorAll('.cell'), event.target);
        boardArray[indexOfClickedCell] = currentPlayer;
        switchPlayer();
             
   
      }
    }
  });
})();


const gameDisplay = document.getElementById("game-board");


// Define reset button function first
function resetGame(){

// Define result, reset the board
//if the board array index is equal to a character
if(boardArray.includes('X','O')){
    { return gameDisplay.innerHTML = "";
       
      }
}
else
  {;}
//and if the cells textContent is equal to a character
//return the result, which is the reset board function 
}

const resetButton = document.querySelector(".restart");
resetButton.addEventListener("click", (event) => {
    resetGame(event);
});

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://use.typekit.net/xtq6hun.css">
    <link rel="stylesheet" href="styles.css">
    <title>TIC-TAC-TOE</title>
</head>
<body>
    <main>
<div >
<h1>TIC TAC TOE</h1>
<div id ="game-text">
</div>
</div>
<div  id="game-board">
<div  id="cell-1"></div>
<div  id="cell-2"></div>
<div  id="cell-3"></div>
<div  id="cell-4"></div>
<div  id="cell-5"></div>
<div  id="cell-6"></div>
<div  id="cell-7"></div>
<div  id="cell-8"></div>
<div  id="cell-9"></div>
</div>
<div >
<button >RESTART GAME</button>
</div>
    </main>
<script src="script.js" defer></script>
</body>
</html>

CSS

/*CSS RESET*/
* {
margin:0;
padding:0;
}

body {
background-color: #faf9fa;
}

main {
display: flex;
align-content: center;
flex-direction: column;
flex-wrap: nowrap;
justify-content: center;
align-items: center;
}

h1 {
font-family: blackest-text, sans-serif;
font-weight: 700;
font-style: normal;
font-size: 60px;
color: #38393a;
}

h2 {
font-family: elza, sans-serif;
font-weight: 500;
font-style: normal;
font-size: 20px;
color: #38393a;
}

.top-text {
border: none;
margin: 0.5em;
display: flex;
flex-direction: column;
align-items: center;
}

.game-board {
display: grid;
grid-template-columns: repeat(3, 1fr);
border: 1px solid #38393a;
margin: 0px auto;
background-color: #faf9fa;
margin-bottom: 2em;
}

.cell:hover {
background-color: rgb(221, 253, 228); 
}

.cell {
/*STYLING X'S AND O'S*/    
font-family: blackest-text, sans-serif;
font-weight: 700;
font-style: normal;
font-size: 100px;
color: #38393a;
/*STYLING INDIVUAL CELLS*/
min-height: 170px;
min-width: 170px;
border: 1px solid #38393a;
display: flex;
align-content: center;
flex-wrap: nowrap;
justify-content: center;
align-items: center;
}

img {
width: 90px;
color:#38393a;
}

.message-display {
border: none;
margin-bottom: 1em;
}

.restart{
font-family: elza, sans-serif;
font-weight: 500;
font-style: normal;
height: 3.5em;
width: 12em;
border-radius: 30px;
border: 1px solid #38393a;
background-color: #faf9fa;
padding: 1px;
}

.restart:hover{
background-color: rgb(221, 253, 228);
}

span {
    font-family: blackest-text, sans-serif;
    font-weight: 700;
    font-style: normal;
    font-size: 100px;
    color: #38393a;
    }

p {
    font-family: blackest-text, sans-serif;
    font-weight: 700;
    font-style: normal;
    font-size: 30px;
    color: #38393a;
}


CodePudding user response:

In your resetGame function you should loop over all the cells and remove their content instead of deleting the whole game-board content.

for(let i =0;i < gameDisplay.children.length;i  ) {
    gameDisplay.children[i].innerHTML = '';
}
  • Related