Change color of button after click

Description

A button will be created by the HTML Fragment. After clicking the button, the class “active” gets added and the button toggles its color according to its status.

 

HTML

<button type=”button” id=”button” class=”buttonClass”>make me red</button>

CSS

.buttonClass{

height: 100px;

width: 200px;

}

.buttonClass.active{

background-color: red;

}

JavaScript

var btn = document.getElementById(“button”);

btn.addEventListener(“touchstart”,

function(){

btn.classList.add(“active”);

});

Screenshots

 

Close Menu
Close Panel