Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

asked on

Detecting Keypress in Javascript

I wrote a small test to try to capture keyup event.

It doesn't work, see attached code.

What's wrong?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title>Check Key Press</title>
<script type="text/javascript">	
document.st.onkeyup = KeyCheck; 
function KeyCheck() {
	var KeyID = document.st.onkeyup.keyCode;
	if (var == 13) {
		alert("Enter pressed");
	} else {
		alert("other key pressed");
	}		
  // -->
</script>	 
</head>

<body>
<form method="post" name="st">
	<input type="text" name="txt" size="3"><br><br>
	<input type="submit" value="Submit">
</form>


</body>
</html>

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

Hello rkorts,

Check this :


<html><head>
<script language="javascript">
	function CommentCharacterLimit(event) {
			document.getElementById("myid").innerHTML = ((window.event)?(window.event.keyCode):(event.which));
	}
</script>
</head>
<body onkeyup="CommentCharacterLimit(event);">
<div id="myid"></div>
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Richard Korts

ASKER

Works good, but I still can't understand why it has to be so complex.
>Works good, but I still can't understand why it has to be so complex.
Because it's not the same code for Netscape like and IE navigator.
Thanks for the points!