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

156
Views
Detect Enter keypress in html input without jquery

I just want to detect the enter input keypress on my android device. I found out that using jquery, we can do like below:

$('#inputText').keypress(function(event) {
var keycode = event.keyCode || event.which;
if(keycode == '13') {
    alert('You pressed a "enter" key in somewhere');    
}
});

But I don't want to use jquery. I want to use the traditional way like using

document.getElementById('inputText')

But I don't know how to add in the keypress event function. Do you guys have any idea?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Almost the same as in jQuery. Use eventListener and pass an argument e to the function to catch the event and it's keyCode.

var elem = document.getElementById('inputText');
elem.addEventListener('keypress', function(e){
  if (e.keyCode == 13) {
    console.log('You pressed a "enter" key in somewhere');   
  }
});
<input id='inputText'>

about 4 years ago · Santiago Trujillo Report

0

document.getElementById("id").onKeyDown = function(event) {

    if (event.keycode === 13) {
        alert("return pressed");
    }

};
about 4 years ago · Santiago Trujillo Report

0

Use event.key instead of event.keyCode!

const node = document.getElementById('inputText');
node.addEventListener('keydown', function onEvent(event) {
    if (event.key === "Enter") {
        // Do something
    }
});

Mozilla Docs

Supported Browsers

about 4 years ago · Santiago Trujillo 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!