Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  147] [ 3]  / answers: 1 / hits: 21807  / 13 Years ago, tue, june 28, 2011, 12:00:00

I googled and got the following codes on the Net.However, when I press a keyboard key,it is not displaying me an alert box. I want to get which character I have pressed in the alert box. How do I fix this?



<script type=text/javascript>

var charfield=document.getElementById(char)
charfield.onkeydown=function(e){
var e=window.event || e;
alert(e.keyCode);
}

</script>
</head>

<body id=char>

</body>
</html>

More From » html

 Answers
15

If you want to get the character typed, you must use the keypress event rather than the keydown event. Something like the following:



var charfield = document.getElementById(char);
charfield.onkeypress = function(e) {
e = e || window.event;
var charCode = (typeof e.which == number) ? e.which : e.keyCode;
if (charCode > 0) {
alert(Typed character: + String.fromCharCode(charCode));
}
};

[#91462] Sunday, June 26, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
josuea

Total Points: 609
Total Questions: 121
Total Answers: 104

Location: South Georgia
Member since Fri, Nov 13, 2020
4 Years ago
josuea questions
;