Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event.keyCode doesn't work with 'on input' - undefined

Currently I'm working on a feature for a project and event.keyCode appears doesn't work for "on input" trigger. I'm using Google Chrome 31 and jQuery 1.10.2.

Here's what I tried inside my method:

input.on('input', function(event){
    console.log("event.charCode: " + event.charCode);
    console.log("event.keyCode: " + event.keyCode);
    console.log("event.which: " + event.which);
    console.log("window.event ? event.keyCode : event.which: " + window.event ? event.keyCode : event.which);
});

enter image description here

I get undefined for all this methods. Am I doing something wrong or this is a jQuery / Javascript issue? Here you can find the fiddle.

I want that my input fires when user paste something inside it or change, by any way, the input content.

p.s: my method works as expected, the only thing that doesn't work is...this.

like image 359
Paladini Avatar asked Jan 16 '14 11:01

Paladini


2 Answers

The event input does not have the keycode, but you can use the inputType property that way.

console.log(event.inputType);

event.inputType == "deleteContentBackward" // Backspace
event.inputType == "deleteContentForward"  // Delete
event.inputType == "insertText"            // When insert character
like image 107
Julio Cesar Brito Gomes Avatar answered Oct 17 '22 00:10

Julio Cesar Brito Gomes


That is because you are using onkeypress instead of onkeydown!

Try doing it with onkeydown and you will be able to access e.keyCode.

so instead of:

<input onkeypress="getKeyValue()"/>

You should do:

<input onkeydown="getKeyValue()"/>

I hope this helps, good luck mate!

like image 29
Dremiq Avatar answered Oct 17 '22 01:10

Dremiq