54

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
  • 13,613
  • 4
  • 67
  • 57
FlyingCat
  • 14,036
  • 36
  • 119
  • 198
  • 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

88
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
  • 398
  • 2
  • 16
sachleen
  • 30,730
  • 8
  • 78
  • 73
19

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,244
  • 5
  • 35
  • 53
14

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
  • 42,564
  • 15
  • 187
  • 127
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,725
  • 1
  • 16
  • 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
  • 77,694
  • 21
  • 158
  • 175
Chris Knight
  • 1,448
  • 1
  • 15
  • 21
0

I use this code and it works fine:

document.getElementById("theIdHere").onkeypressed = 
function() {
     if(this.event.which === 13)
        console('passed');
}
A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
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
  • 13,196
  • 5
  • 87
  • 72