DEV Community

Discussion on: How to make a button looked like it's staying pressed down

Collapse
fiddeou profile image
Esteban Fiddeou

How can I make the buttons stay pressed for a specific time, and then return?

Collapse
nicm42 profile image
Nic Author

setTimeout is your friend here. You can add it into the click listener after you set the button to look pressed:

  button.classList.add('active');
  setTimeout(() => { button.classList.remove('active'); }, 2000);
Enter fullscreen mode Exit fullscreen mode

The 2000 there is the number of milliseconds it should wait before it executes. 2000ms is 2 seconds.

You can see the whole thing in the Codepen at codepen.io/nicm42/pen/OJjdoqe

Collapse
fiddeou profile image
Esteban Fiddeou

Cool thanks!