Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"Selected" Button Animation/Style

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">

Example

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading