Is it possible to change the CSS class of an HTML button using JavaScript?
I tried this but it didn’t work:
document.getElementById("btnChoice1").setAttribute("class", "redButton");
HTML Button
<button class="basicButton" id="btnChoice1">
CSS
.basicButton {
width:300px;
}
.redButton {
width:300px;
background-color:red;
}
classNameis preferred but yoursetAttribute()code should work. If it’s not working then you probably have the Javascript executing before the button exists on the page.Check that the code is within an
window.onloadevent or is after the button markup.