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

Richard KortsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
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

0
leakim971PluritechnicianCommented:
So in your code :


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>Check Key Press</title>
<script type="text/javascript"> 
function KeyCheck(event) {
        var KeyID = ((window.event)?(window.event.keyCode):(event.which));
        if (var == 13) {
                alert("Enter pressed");
        } else {
                alert("other key pressed");
        }               
  // -->
</script>        
</head>
<body onkeyup="KeyCheck(event);">
<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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richard KortsAuthor Commented:
Works good, but I still can't understand why it has to be so complex.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

leakim971PluritechnicianCommented:
>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.
0
leakim971PluritechnicianCommented:
Thanks for the points!
0
leakim971PluritechnicianCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.