• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 650
  • Last Modified:

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

0
Richard Korts
Asked:
Richard Korts
  • 5
1 Solution
 
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
 
Richard KortsAuthor Commented:
Works good, but I still can't understand why it has to be so complex.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now