Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
148
rated 0 times [  154] [ 6]  / answers: 1 / hits: 49898  / 13 Years ago, tue, april 12, 2011, 12:00:00

I'm getting codes [96..105] by calling String.fromCharCode(event.keyCode) when pressing keys [0..9](digits) on the keypad.
Though these codes correspond to characters: 'a b c d e f g h i' instead of [0..9].



Question:



I have 3 inputs in the form. User allowed to enter only in the 1-st input. While user press keys on keyboard some function need to filter it and write it to 2-nd input if pressed key is digit otherwise it must write it to the 3-rd input. How it can be corrected?



My implementation in JSFiddle


More From » jquery

 Answers
18

Use the keypress handler:



[somelement].onkeypress = function(e){
e = e || event;
console.log(String.fromCharCode(e.keyCode));
}


See also: this W3C testdocument



if you want to use the keyup or keydown handler, you can subtract 48 from e.keyCode to get the number (so String.fromCharCode(e.keyCode-48))


[#92794] Monday, April 11, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
raveno

Total Points: 453
Total Questions: 92
Total Answers: 92

Location: France
Member since Thu, Oct 27, 2022
2 Years ago
raveno questions
;