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

195
Views
How to trigger event with enter as if element is clicked?

So because I need to workaround some built-in form functionality in software that we use, I want to trigger a click on an element, by pressing enter.

This is my script:

let submitButton = document.querySelector('.test');

submitButton.addEventListener('click', submitForm, false);

submitButton.addEventListener('keydown', function(key) {
  if (key.keyCode == '13') {
    key.preventDefault();
    submitButton.click();
  }
});

function submitForm() {
  // do something
}

Clicking on the div with class="test" triggers an external script which only gets triggered on button click. Now for user experience I want to trigger this aswell on press of enter. According to documentation it should be like the above, but pressing enter doesn't trigger the script.

Does anybody know how to fix this?

about 4 years ago · Juan Pablo Isaza
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!