Button moving when adding text dynamically in JavaScript

Viewed 18

I have two files.

var count = 0;
var One = document.getElementById("One");
var Two = document.getElementById("Two");
var Three = document.getElementById("Three");
var Four = document.getElementById("Four");
var Five = document.getElementById("Five");
var Six = document.getElementById("Six");
var Seven = document.getElementById("Seven");
var Eight = document.getElementById("Eight");
var Nine = document.getElementById("Nine");

function eventListener(newButton) {
  newButton.addEventListener("click", function() {
    if (count % 2 == 0) {
      newButton.innerHTML = "O";
    } else if (count % 2 == 1) {
      newButton.innerHTML = "X";
    }
    count++;
    setTimeout(() => {
      gameStatus();
    }, "50")
  })
}

function gameStatus() {
  if ((One.textContent === "O" && Two.textContent === "O" && Three.textContent === "O") || (Four.textContent === "O" && Five.textContent === "O" && Six.textContent === "O") || (Seven.textContent === "O" && Eight.textContent === "O" && Nine.textContent === "O") || (One.textContent === "O" && Four.textContent === "O" && Seven.textContent === "O") || (Two.textContent === "O" && Five.textContent === "O" && Eight.textContent === "O") || (Three.textContent === "O" && Six.textContent === "O" && Nine.textContent === "O") || (One.textContent === "O" && Five.textContent === "O" && Nine.textContent === "O") || (Three.textContent === "O" && Five.textContent === "O" && Seven.textContent === "O")) {
    window.alert("Player One Won");
  } else if ((One.textContent === "X" && Two.textContent === "X" && Three.textContent === "X") || (Four.textContent === "X" && Five.textContent === "X" && Six.textContent === "X") || (Seven.textContent === "X" && Eight.textContent === "X" && Nine.textContent === "X") || (One.textContent === "X" && Four.textContent === "X" && Seven.textContent === "X") || (Two.textContent === "X" && Five.textContent === "X" && Eight.textContent === "X") || (Three.textContent === "X" && Six.textContent === "X" && Nine.textContent === "X") || (One.textContent === "X" && Five.textContent === "X" && Nine.textContent === "X") || (Three.textContent === "X" && Five.textContent === "X" && Seven.textContent === "X")) {
    window.alert("Player Two Won");
  } else if (One.textContent !== " " && Two.textContent !== " " && Three.textContent !== " " && Four.textContent !== " " && Five.textContent !== " " && Six.textContent !== " " && Seven.textContent !== " " && Eight.textContent !== " " && Nine.textContent !== " ") {
    window.alert("Draw");
  }
}
eventListener(One);
eventListener(Two);
eventListener(Three);
eventListener(Four);
eventListener(Five);
eventListener(Six);
eventListener(Seven);
eventListener(Eight);
eventListener(Nine);
html {
  background-image: url(../images/background.png);
  background-size: cover;
  background-repeat: no-repeat;
}

body {
  margin: 0px;
}

#NavBar {
  background-color: #1f1f1f;
  width: 100%;
  overflow: auto;
  margin-top: 0px;
}

h2 {
  color: white;
  display: inline-block;
  margin-top: 0px;
  margin-bottom: 0px;
}

#NavLinks {
  float: right;
  margin: 0 auto;
}

a {
  color: white;
  display: block;
  padding: 14px 16px;
  text-decoration: none;
}

#Website {
  padding: 14px 16px;
}

#Board {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 300px;
  height: 300px;
  font-size: 0;
}

button {
  height: 100px;
  width: 100px;
  padding: 0px;
  border-color: black;
  border-style: solid;
  border-width: 2px;
  color: black;
  font-size: 30px;
}

#One {
  border-left-width: 0px;
  border-top-width: 0px;
}

#Two {
  border-top-width: 0px;
}

#Three {
  border-top-width: 0px;
  border-right-width: 0px;
}

#Four {
  border-left-width: 0px;
}

#Six {
  border-right-width: 0px;
}

#Seven {
  border-left-width: 0px;
  border-bottom-width: 0px;
}

#Eight {
  border-bottom-width: 0px;
}

#Nine {
  border-bottom-width: 0px;
  border-right-width: 0px;
}
<div id="NavBar">
  <h2 id="Website"> My Website </h2>
  <div id="NavLinks">
    <h2><a href="index.html"> Home </a></h2>
    <h2><a href="projects.html"> Projects </a></h2>
    <h2><a href="tictactoe.html"> Tic Tac Toe </a></h2>
  </div>
</div>
<div id="Board">
  <button id="One"> </button>
  <button id="Two"> </button>
  <button id="Three"> </button>
  <button id="Four"> </button>
  <button id="Five"> </button>
  <button id="Six"> </button>
  <button id="Seven"> </button>
  <button id="Eight"> </button>
  <button id="Nine"> </button>
</div>

Basically in the eventListener function, every time I set the text of a button, it moves the button I set the text of downwards. That causes everything now in the same row to move downwards as well. However, once the entire row is full of text, the positions fix themselves. It's just supposed to be a simple tic tac toe game Example

0 Answers
Related