I am creating a sudoku gameboard and I need to draw some lines to divide the 3x3 boxes. The board is made out of buttons, so what I am trying to do is change the border color from the buttons on the 3rd and 6th column, & line.
The problem I am facing is that I can't add on a button 2 borders: right and bottom. The Js code only adds one attribute at a time, using elem.setAttribute
. So I tried a function that supposedly lets you add more than 1 attribute, but it doesn't seem to work.
Where is the bug?
function createGameboard() {
var gameboard = document.getElementById("gameboard");
console.log("creating buttons");
for (var i = 1; i <= 9; i++) {
for (var j = 1; j <= 9; j++) {
var button = document.createElement("button");
button.id = i + "-" + j;
button.className = "buttons";
button.innerHTML = i + "-" + j;
if (i == 3 || i == 6) {
button.setAttribute("style", "border-bottom: 4px solid black");
}
if (j == 3 || j == 6) {
button.setAttribute("style", "border-right: 4px solid black");
}
if (
(i == 3 && j == 3) ||
(i == 6 && j == 6) ||
(i == 3 && j == 6) ||
(i == 6 && j == 3)
) {
setAttributes(button, {
"border-right": "4px solid black",
"border-bottom": "4px solid black",
});
}
gameboard.appendChild(button);
}
}
}
createGameboard();
function setAttributes(el, attrs) {
for (var key in attrs) {
el.setAttribute(key, attrs[key]);
}
}
body {
background-color: #0f4c5c;
}
h1,
h5 {
color: white;
}
.sudoku #gameboard {
width: 40vmin;
height: 40vmin;
display: grid;
grid-template-columns: repeat(9, 1fr);
gap: 0;
}
.sudoku button {
width: 10vmin;
height: 10vmin;
background: white;
border: 3px solid gray;
margin: 0;
}
<body>
<div class="container">
<div class="col">
<h1 class="row mx-auto my-3" id="title">
Sudoku
</h1>
<div class="row my-2 container sudoku">
<div class="gameboard" id="gameboard"></div>
</div>
</div>
</div>
</div>
</body>
If you only use setAttribute
for styling, you could fix it like this:
function setAttribute(element, key, value) {
element.style[key] = value
}
setAttribute(document.body, "backgroundColor", "red")
Note that you need to use the javascript version of styling (backgroundColor instead of background-color).