I was curious about how I could make a normal button, with a "selected" style or animation.
<button> I'm a button </button>
When you use Radiobuttons, you can see clearly that you have a selected style whenever you click on the button.
<input type="radio">
Now is the question, is it possible to have a selected style or animation (Not a click/hover animation) for the last button that you clicked (Without obviously having to use radiobuttons/checkbox).
If so, how does one make this?
JSFiddle if you want to use the code that I used in the GIF
I haven’t found a lot of articles about this, or maybe just haven’t looked good enough, anyways, maybe you people know how to do this?
>Solution :
Just toggle a class with an onclick event:
btnState = false;
btn.addEventListener("click", () => {
btn.classList.toggle("selected");
btnState = !btnState;
});
In the CSS add:
.selected {
background-color: red;
}
And add an id="btn" to the button in the HTML.
