JavaScript: Avoiding Coded Codes

Possible duplicate:
JavaScript constants event.keyCode

This is my code:

$button.on('keyup', function (event) { // Detect an Enter keypress if(event.keyCode === 13) { doStuff(); } }); 

As you can see, key code 13 hard-coded. Is there a (cross-browser) way to output this number in a more semantically meaningful way?

+4
source share
2 answers

Repeat Alex K. answer (I used):

 "\r".charCodeAt(0) 
+1
source

If you are working with jQueryUI , you can use the $.ui.keyCode constants:

 keyCode: { BACKSPACE: 8, COMMA: 188, DELETE: 46, DOWN: 40, END: 35, ENTER: 13, ESCAPE: 27, HOME: 36, LEFT: 37, NUMPAD_ADD: 107, NUMPAD_DECIMAL: 110, NUMPAD_DIVIDE: 111, NUMPAD_ENTER: 108, NUMPAD_MULTIPLY: 106, NUMPAD_SUBTRACT: 109, PAGE_DOWN: 34, PAGE_UP: 33, PERIOD: 190, RIGHT: 39, SPACE: 32, TAB: 9, UP: 38 } 

So, to check the Enter press:

 if (event.keyCode === $.ui.keyCode.ENTER) { ... } 
+10
source

All Articles