0

I have the following function that resets a button and applies styling once it is executed:

function reset(){
    var boxes = getBoxes();
    for(i = 0 ; i < boxes.length; i++) {
        boxes[i].innerHTML = "";
    }
document.getElementById("start_button").innerHTML = "Start Game";
document.getElementById("start_button").setAttribute('onclick', 'gameStart()');
document.getElementById("start_button").style.color = "black";
document.getElementById("start_button").style.backgroundColor = "lightgreen";

}

However, as you can see, it is getting repetitive to change each style element in the function. Especially if I want this style to be applied:

#button{
    text-align: center;
}
#start_button{
    width: 10%;
    height: 50px;
    margin-top: 4%;
    margin-left: auto;
    margin-right: auto;
    background: lightgreen;
    color:black;
}

#start_button:hover {
    background: green;
    color: white;
}

Is there a way in JavaScript to link to an entire block of style code?

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
alexp2603
  • 355
  • 1
  • 4
  • 13

1 Answers1

0

If you change the start_button to a class; which it should be because there will be more than one of them on the page; you can use the className property.

CSS:

.start_button{
    width: 10%;
    height: 50px;
    margin-top: 4%;
    margin-left: auto;
    margin-right: auto;
    background: lightgreen;
    color:black;
}

.start_button:hover{
    background:green;
    color: white;
}

Look like you are creating buttons so you can grab the buttons JS:

document.getElementsByTagName("button").className = "start_button";
andre mcgruder
  • 1,120
  • 1
  • 9
  • 12