User types a bunch of number keys than presses the enter key. When the enter key is pressed it will show all those keys that were just pressed as a whole string. If someone enters the 'backspace' than we can do something like:
str = str.substring(0, str.length - 1);
I have tried my hands at all sorts of GOOGLE terms and im stumped. Im in desperate need of advice.
Code:
$(function() {
var str = ""; //Variable to be displayed
$(document.body).keyup(function(e) {
if ( ( e.which >= 48 ) && ( e.which <= 57 ) ) { //Test only numbers
str += String.fromCharCode(e.keyCode); //Add ascii codes into a string
if ( e.which === 13 ) { //Test for the enter key
console.log(str); //Display the string of chars that were typed
}
str = ""; //Reset the var back to "";
} else { //Other than number typed
e.preventDefault(); //Stoping, 'return' causes errors in console.
}
});
});

New Topic/Question
Reply



MultiQuote





|