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

428
Views
How to Create a button to execute code when pressing enter or clicking the button?

I have created a button. right now the button only works after clicking the button. what I want to do is, When I press the 'enter' key on the keyboard or click the button my code needs to be executed.

how to do that?

const befoClick= document.querySelector('.befoClick');
const afteClick=document.querySelector('.afteClick');

befoClick.addEventListener('click',()=>{
    afteClick.classList.toggle('clicked'); 
 });
body{
  display: grid;
  place-items: center;
  height:100vh;  
}

button{
  width:200px;
  height:50px;
  font-size:20px;
  cursor: pointer;
  border:none;
   border-radius: 8px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%); 
}
.befoClick{
  background-color: rgb(74, 74, 255);
}

.afteClick{
   background-color: rgb(67, 255, 77);
  visibility: hidden;
}

.clicked{
    visibility: visible;
}

.bfrClick{
    visibility: hidden;
}
<div class='allBtn'>
<div >
  <button class='befoClick'>CLICK ME</button>
  </div>
<div >
  <button class='afteClick'>CLICKED</button>
  </div>
  
  </div>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use the keyup event listener and then check the pressed key.

There are two event listeners so, I added a separate function.

const befoClick= document.querySelector('.befoClick');
const afteClick=document.querySelector('.afteClick');

const toggleClass = () => afteClick.classList.toggle('clicked'); 

befoClick.addEventListener('click', toggleClass);
 
document.addEventListener('keyup', (event) => {
  if (event.key === 'Enter') {
    toggleClass();
  }
});
body{
  display: grid;
  place-items: center;
  height:100vh;  
}

button{
  width:200px;
  height:50px;
  font-size:20px;
  cursor: pointer;
  border:none;
   border-radius: 8px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%); 
}
.befoClick{
  background-color: rgb(74, 74, 255);
}

.afteClick{
   background-color: rgb(67, 255, 77);
  visibility: hidden;
}

.clicked{
    visibility: visible;
}

.bfrClick{
    visibility: hidden;
}
<div class='allBtn'>
<div >
  <button class='befoClick'>CLICK ME</button>
  </div>
<div >
  <button class='afteClick'>CLICKED</button>
  </div>
  
  </div>

about 4 years ago · Juan Pablo Isaza Report

0

we can use an array like ['click', 'keyup] and then use forEach on the array to listen to both Events with the same eventListener.

However, if you use Enter it will trigger the eventListener twice as it recognizes the click and the Enter press itself. As such we need to run the script then also twice.

Then in the actual script, you can use classList.toggle to toggle between a clicked class to apply style changes.

Then you can toggle the textContent of the button between an array aswell.

var eleButton = document.querySelector('.button'),
  buttonText = ['CLICK ME', 'CLICKED'],
  indexButtonText = 0;

['click', 'keyup'].forEach(el => {
  eleButton.addEventListener(el, function(e) {
    changeButton();
    if (e.key == 'Enter') {
      changeButton();
    }
  });
});

function changeButton() {
  indexButtonText = (indexButtonText == 0 ? 1 : 0);
  eleButton.classList.toggle('clicked');
  eleButton.textContent = buttonText[indexButtonText];
}
body {
  display: grid;
  place-items: center;
  height: 100vh;
}

button {
  width: 200px;
  height: 50px;
  font-size: 20px;
  cursor: pointer;
  border: none;
  border-radius: 8px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: rgb(74, 74, 255);
}

.clicked {
  background-color: rgb(67, 255, 77);
}
<button class='button'>CLICK ME</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!