JS toggleClass start
Toggle class name on click in JS
by Marc Chehab
HTML
<div id="banner-message" class="alt">
<p>Hello World</p>
<button onclick="change()">Change color</button>
</div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#banner-message {
background: #fff;
border-radius: 4px;
padding: 20px;
font-size: 25px;
text-align: center;
margin: 0 auto;
width: 300px;
}
button {
background: #0084ff;
border: none;
border-radius: 5px;
padding: 8px 14px;
font-size: 15px;
color: #fff;
}
#banner-message.alt {
background-color: blue;
color: white;
}
.alt button {
background: #ffffff;
color: #000000;
}
JavaScript
function change() {
var elem = document.getElementById("banner-message");
elem.classList.toggle("alt");
}