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

94
Views
Trigger the enter key on button click

I've seen solutions to triggering a button when hitting the enter key, but what I would like to achieve is the opposite. How can I trigger the enter key on button click using JS?

const triggerEnterKey = () => {
  ...
}
<button onClick={()=> {triggerEnterKey()}}>Trigger enter key</button>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use dispatchEvent to dispatch a

new KeyboardEvent('keydown',{'key':'Enter'})
about 4 years ago · Juan Pablo Isaza Report

0

Please try this.

var evt = new KeyboardEvent('keydown', {'keyCode':13, 'which':13}); 
const textTag = document.getElementById("text");                         
                            
window.addEventListener('click', () => {
    document.dispatchEvent (evt); 
  textTag.innerHTML = "Enter was presseed";
});                      
<h1>Simulate enter press on click</h1>
<p id="text"></p>

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!