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

Javascript Detection of a Pressed Enter key

Is there any event in javascript for detecting when an enter key is pressed in a textbox?
0
onaled777
Asked:
onaled777
  • 4
  • 2
  • 2
  • +2
3 Solutions
 
HonorGodCommented:
Something like this, perhaps.

For an enter key, check for a value of 13, instead of 27
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> The Great Escape </title>
<script type='text/javascript'>
  function KeyCheck( event ) {
    if ( window.event ) {
      event = window.event;
    }
    var key = event.keyCode;
    if ( key == 27 ) {
      alert( 'There is no escape.' );
    } else {
      alert( 'keyCode: type=' + typeof( event.keyCode ) + ' = ' + event.keyCode );
    }
  }
  window.onload = function() {
    var here = document.getElementById( 'here' );
    if ( here ) {
      here.focus();
      here.onkeyup    = KeyCheck;
//    here.onkeypress = KeyCheck;
//    here.onkeydown  = KeyCheck;
      alert( 'ok' );
    } else {
      alert( '"here" not found.' );
    }
  }
</script>
</head>
<body>
  <input type='text' id='here'>
</body>
</html>

Open in new window

0
 
leakim971PluritechnicianCommented:
Hello onaled777,

Work for me :

<html><head>
<script language="Javascript">
	document.onkeydown = function() {
		if (event.keyCode == 13) {
			alert("you press RETURN");
		}
	}
</script>
</head>
<body>
</body></html>

Open in new window

0
 
jet-blackCommented:
you need keycode for that:
http://www.quirksmode.org/js/keys.html
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
onaled777Author Commented:
Thanks leakim971

But that seems attached to the document object.  Anyway to make it attached to a text box input?


0
 
MorcalavinCommented:
Dont' use leakim971 solution.  It doesn't work anyway.

HonorGod's solution should point you in the right direction.
0
 
leakim971PluritechnicianCommented:
Try this :


<html><head>
<script language="Javascript">
	function okd(event) {
		if( ((window.event)?(window.event.keyCode):(event.which)) == 13 ) {
			alert("you press RETURN");
		}
	}
</script>
</head>
<body>
<input id="mytxt" name="mytxt" onkeydown="okd()" />
</body></html>

Open in new window

0
 
onaled777Author Commented:
I am making some progress with these but in IE, not firefox.  Can anyone show me how to get these events to work in Firefox?
0
 
leakim971PluritechnicianCommented:
Forget to pass event :
<input id="mytxt" name="mytxt" onkeydown="okd(event)" />

<html><head>
<script language="Javascript">
	function okd(event) {
		if( ((window.event)?(window.event.keyCode):(event.which)) == 13 ) {
			alert("you press RETURN");
		}
	}
</script>
</head>
<body>
<input id="mytxt" name="mytxt" onkeydown="okd(event)" />
</body></html>

Open in new window

0
 
leakim971PluritechnicianCommented:
Thanks for the points!
0
 
HonorGodCommented:
Thanks for the grade & points.

Good luck & have a great day.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now