Edit part of a div created using .createElement in js rather than duplicating the whole line

Viewed 24

Im making a rock paper scissors game using js and html. Right now I'm having an issue with displaying the computers random choice. I created the text that appears purley through js with the .createElement method and it partially works. Right now this is what I'm getting:

enter image description here

I don't want to add a new line every time the player clicks a button. I just want to update the part with the orange box around it:

enter image description here

I have looked online for solutions but couldn't make sense of the solutions so any help with this would be greatly appreciated.

const getComputerChoice = () => {
  let optionsArray = ["rock", "paper", "scissors"];
  let randomNumber = Math.floor(Math.random() * optionsArray.length);

  let compChoiceText = document.createElement("div");
  compChoiceText.className = "font-light text-white text-4xl";


  compChoiceText.innerHTML = `<p>
      Computer chose: <span>${optionsArray[randomNumber]}</span>
    </p>`;
  document.getElementById("text-output-container").appendChild(compChoiceText);
};


const choice = document.querySelector(".buttons");
choice.addEventListener("click", (event) => {
  getComputerChoice();
});
<!DOCTYPE html>

<html>

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>ROCK PAPER SCISSORS</title>
  <meta name="description" content="" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <link rel="stylesheet" href="" />
  <script src="https://cdn.tailwindcss.com"></script>
</head>

<body>
  <div class="w-full h-screen flex flex-col items-center justify-center bg-black gap-y-12">
    <div id="text-output-container"></div>
    <div class="buttons w-fit flex gap-x-10">
      <button class="w-80 bg-cyan-500 shadow-lg shadow-cyan-500/50 text-white rounded text-6xl hover:bg-blue-500 hover:shadow-blue-500/50">
          ROCK
        </button>
      <button class="w-80 bg-cyan-500 shadow-lg shadow-cyan-500/50 text-white rounded text-6xl hover:bg-blue-500 hover:shadow-blue-500/50">
          PAPER
        </button>
      <button class="w-80 bg-cyan-500 shadow-lg shadow-cyan-500/50 text-white rounded text-6xl hover:bg-blue-500 hover:shadow-blue-500/50">
          SCISSORS
        </button>
    </div>
  </div>
  <script src="script.js" async defer></script>
</body>

</html>

2 Answers

instead of creating the element every time, creare it once in the html and edit it evertime the user clicks

const getComputerChoice = () => {
  let optionsArray = ["rock", "paper", "scissors"];
  let randomNumber = Math.floor(Math.random() * optionsArray.length);
  // edit here
  let compChoiceText = document.getElementById("computer-response");
  
  compChoiceText.innerHTML = `<p>
      Computer chose: <span>${optionsArray[randomNumber]}</span>
    </p>`;
  document.getElementById("text-output-container").appendChild(compChoiceText);
};


const choice = document.querySelector(".buttons");
choice.addEventListener("click", (event) => {
  getComputerChoice();
});
<!DOCTYPE html>

<html>

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>ROCK PAPER SCISSORS</title>
  <meta name="description" content="" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <link rel="stylesheet" href="" />
  <script src="https://cdn.tailwindcss.com"></script>
</head>

<body>
  <div class="w-full h-screen flex flex-col items-center justify-center bg- 
    black gap-y-12">
    <div id="text-output-container">
       <!-- added this once -->
       <div id="computer-response" calss="font-light text-white text-4xl"> 
       </div>
    </div>
    <div class="buttons w-fit flex gap-x-10">
      <button class="w-80 bg-cyan-500 shadow-lg shadow-cyan-500/50 text-white rounded text-6xl hover:bg-blue-500 hover:shadow-blue-500/50">
          ROCK
        </button>
      <button class="w-80 bg-cyan-500 shadow-lg shadow-cyan-500/50 text-white rounded text-6xl hover:bg-blue-500 hover:shadow-blue-500/50">
          PAPER
        </button>
      <button class="w-80 bg-cyan-500 shadow-lg shadow-cyan-500/50 text-white rounded text-6xl hover:bg-blue-500 hover:shadow-blue-500/50">
          SCISSORS
        </button>
    </div>
  </div>
  <script src="script.js" async defer></script>
</body>

</html>

Figured it out. Each time the getCompChoice function was called I was creating a new div so all I needed to do was create the div once then edit the variable.

let compChoiceText;

const getComputerChoice = () => {
  let optionsArray = ["rock", "paper", "scissors"];
  let randomNumber = Math.floor(Math.random() * optionsArray.length);
  if (!compChoiceText) {
  compChoiceText = document.createElement("div");
  compChoiceText.className = "font-light text-white text-4xl";
  }
  console.log(optionsArray[randomNumber]);

  compChoiceText.innerHTML = `<p>
      Computer chose: <span>${optionsArray[randomNumber]}</span>
    </p>`;
  document.getElementById("text-output-container").appendChild(compChoiceText);
};


const choice = document.querySelector(".buttons");
choice.addEventListener("click", (event) => {
  getComputerChoice();
});
<!DOCTYPE html>

<html>

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>ROCK PAPER SCISSORS</title>
  <meta name="description" content="" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <link rel="stylesheet" href="" />
  <script src="https://cdn.tailwindcss.com"></script>
</head>

<body>
  <div class="w-full h-screen flex flex-col items-center justify-center bg-black gap-y-12">
    <div id="text-output-container"></div>
    <div class="buttons w-fit flex gap-x-4">
      <button class="w-24 h-fit bg-cyan-500 shadow-lg shadow-cyan-500/50 text-white rounded text-md hover:bg-blue-500 hover:shadow-blue-500/50">
          ROCK
        </button>
      <button class="w-24 h-fit bg-cyan-500 shadow-lg shadow-cyan-500/50 text-white rounded text-md hover:bg-blue-500 hover:shadow-blue-500/50">
          PAPER
        </button>
      <button class="w-24 h-fit bg-cyan-500 shadow-lg shadow-cyan-500/50 text-white rounded text-md hover:bg-blue-500 hover:shadow-blue-500/50">
          SCISSORS
        </button>
    </div>
  </div>
  <script src="script.js" async defer></script>
</body>

</html>

Related