Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
How to trigger button with keyboard and active

I want to trigger a button with keyboard and with mouse. It works, but the :active does not show if I trigger it with keyboard.

HTML

<button id="btn1" onclick="btn1()"> click </button>

CSS

#btn1:active{
transform:scale(1.1);
}

Javascript

document.addEventListener("keydown", function(event) {
    if (event.key === '1') {
      document.getElementById("btn1").click();
    }
});
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You'll need to use Javascript add/remove class for this use case.

In the keydown eventListner add the active class. Add another eventListner for keyup and then remove that active class.

document.addEventListener("keydown", function(event) {
      var btn = document.getElementById("btn1");
      btn.click();
      btn.classList.add('active');
});
document.addEventListener("keyup", function(event) {
      var btn = document.getElementById("btn1");
      btn.classList.remove('active');
});

function btn1(){
  
}
#btn1:active{
transform:scale(1.1);
}
.active{
transform:scale(1.1);
}
<button id="btn1" onclick="btn1()"> click </button>

about 4 years ago · Juan Pablo Isaza Report

0

You can use selector.style

and use setTimeout() to transfrom to default scale

Example :

document.addEventListener("keydown", function(event) {
    if(event.key == "1"){
        clicked(document.getElementById("btn1"));
        document.getElementById("btn1").click();
    }
});

function clicked(el){
    el.style.transform = "scale(1.1)";
    setTimeout(()=>{ el.removeAttribute('style'); }, 100);
}
#btn1:active{
  transform:scale(1.1);
}
<button id="btn1" onclick="//btn1()"> click </button>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!