How to set multiple attributes on the same element

Viewed 690

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>

3 Answers

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).

It is because you override the attribute. You have to concatenate the attributes. like that:

const d = document.querySelector('button')
console.log('before',d)

d.setAttribute("style", "border-bottom: 4px solid green;")
_tmp = d.getAttribute("style")
d.setAttribute("style", _tmp + " border-right: 4px solid black")

console.log('after', d)
<button>1</button>

I use the function not only for styling but also for adding/removing class.

const setAttributes = (el, object) => {
  for (let key in object) {
    if (key === "addClass") {
      el.classList.add(object[key]);
    } else if (key === "removeClass") {
      el.classList.remove(object[key]);
    } else {
      el.setAttribute(key, object[key]);
    }
  }
};

The usage is quite simple:

setAttributes(cBtn, {
  class: `${this.prefix}-clear hidden`,
  type: "button",
  "aria-label": this.clearBtnAriLabel,
});
Related