?
Solved

Javascript Detection of a Pressed Enter key

Posted on 2010-01-12
10
Medium Priority
?
557 Views
Last Modified: 2012-05-08
Is there any event in javascript for detecting when an enter key is pressed in a textbox?
0
Comment
Question by:onaled777
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 41

Accepted Solution

by:
HonorGod earned 336 total points
ID: 26297526
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
 
LVL 82

Expert Comment

by:leakim971
ID: 26297536
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
 
LVL 12

Assisted Solution

by:jet-black
jet-black earned 332 total points
ID: 26297537
you need keycode for that:
http://www.quirksmode.org/js/keys.html
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:onaled777
ID: 26297588
Thanks leakim971

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


0
 
LVL 18

Expert Comment

by:Morcalavin
ID: 26297639
Dont' use leakim971 solution.  It doesn't work anyway.

HonorGod's solution should point you in the right direction.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 26299307
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
 

Author Comment

by:onaled777
ID: 26303885
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
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 332 total points
ID: 26303999
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
 
LVL 82

Expert Comment

by:leakim971
ID: 26337195
Thanks for the points!
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 26337242
Thanks for the grade & points.

Good luck & have a great day.
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question