51

I only have 1 input field and I want to register onChange and onKeyPress event to detect when the users finish their input or press the Enter key. I need to use javascript only. Thanks for the help.

I have:

var load = function (){
   //I want to trigger this function when user hit Enter key.
}

document.getElementById('co').onchange=load;   //works great
document.getElementById('co').onKeyPress=load; 
//not sure how to detect when user press Enter

html

//no form just a single input field
<input type='text' id='co'>
Peter Tseng
  • 12,827
  • 3
  • 65
  • 56
FlyingCat
  • 13,696
  • 36
  • 114
  • 188
  • Possible duplicate of [Enter key press event in JavaScript](http://stackoverflow.com/questions/905222/enter-key-press-event-in-javascript) – Bae Mar 23 '16 at 21:16

8 Answers8

84
document.getElementById('foo').onkeypress = function(e){
    if (!e) e = window.event;
    var keyCode = e.code || e.key;
    if (keyCode == 'Enter'){
      // Enter pressed
      return false;
    }
  }

DEMO

Daniel
  • 308
  • 2
  • 14
sachleen
  • 29,893
  • 8
  • 74
  • 71
17

None of these answers as of yet specifically answer this question. The question is in 2 parts. 1. Enter is Pressed. 2. The uses is focused on an input.

Jquery

$('#input-id').on("keyup", function(e) {
    if (e.keyCode == 13) {
        console.log('Enter');
    }
});

Vanilla JavaScript

inputId = document.getElementById('input-id');
inputId.addEventListener('keyup', function onEvent(e) {
    if (e.keyCode === 13) {
        console.log('Enter')
    }
});

This way ENTER is only detected when the user presses enter FROM that input.

Case
  • 4,122
  • 5
  • 33
  • 50
11

More recent and much cleaner: use event.key instead of event.keyCode. No more arbitrary number codes!

const node = document.getElementById('co');
node.addEventListener('keydown', function onEvent(event) {
    if (event.key === "Enter") {
        return false;
    }
});

Mozilla Docs

Supported Browsers

Gibolt
  • 33,561
  • 12
  • 157
  • 107
7

To make it cross browser:

document.getElementById('foo').onkeypress = function(e) {
    var event = e || window.event;
    var charCode = event.which || event.keyCode;

    if ( charCode == '13' ) {
      // Enter pressed
      return false;
    }
}

See this question for more details: javascript event e.which?

Community
  • 1
  • 1
SpacedMonkey
  • 2,685
  • 1
  • 14
  • 17
1

use the event.keyCode

var code = (e.keyCode ? e.keyCode : e.which);
 if(code == 13) { //Enter keycode
   //Do something
 }

There is already a discussion in this link jQuery Event Keypress: Which key was pressed?

Community
  • 1
  • 1
0

Here is a good link that discusses this topic and provides a working examples

function DetectEnterPressed(e) {
var characterCode
if(e && e.which){ // NN4 specific code
e = e
characterCode = e.which
}
else {
e = event
characterCode = e.keyCode // IE specific code
}
if (characterCode == 13) return true // Enter key is 13
else return false
}

http://hspinfo.wordpress.com/2008/09/01/using-javascript-detect-enter-key-pressed-event-in-a-textbox-and-perform-the-action/

kapa
  • 75,446
  • 20
  • 155
  • 173
Chris Knight
  • 1,398
  • 1
  • 13
  • 21
0

I use this code and it works fine:

document.getElementById("theIdHere").onkeypressed = 
function() {
     if(this.event.which === 13)
        console('passed');
}
A-Sharabiani
  • 15,608
  • 15
  • 99
  • 122
0

These day you can do it, in this way:

document.getElementById('co').addEventListener('keydown',
  e => !e.repeat && e.keyCode === 13 && load());

Please notice that load() will run it once.

Daniel De León
  • 12,505
  • 5
  • 78
  • 69