Solved

Enter key to respond in a text box

Posted on 1998-10-28
6
159 Views
Last Modified: 2008-03-10
I want a text box which is password type should be able to respond an enter key i.e; on pressing the enter key another page needs to be called. I am also not able to trap the ASCII values  of keys pressed from the keyboard in Javascript.
0
Comment
Question by:PSV
  • 3
  • 3
6 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 100 total points
ID: 1273092
<html>
<head>
<script>
NS = (document.layers);
IE = (document.all);
function f1(e) { // This takes over the enter key in the specified field
   if((NS && e.which == 13) || (IE && window.event.keyCode == 13)) {
      document.myForm.submit();
      return false; // cancel the enter (Dings in Netscape)
   }
}
</script>
</head>
<BODY>
Here is a simple code for submitting a form with multiple fields.
The form is submitted by hitting enter in field 1 by assigning a fuction to the
document.form.field.onKeypress.
<FORM name=myForm action="enter.html">
1.<INPUT TYPE="PASSWORD" NAME="FirstField"  size=40><BR>
<BR>2.<INPUT TYPE="text" NAME="SecondField"  size=40>
</FORM>
<SCRIPT LANGUAGE="JavaScript1.1">
   document.myForm.FirstField.onKeyPress = f1;
</script>

<P>use document.onkeypress = keyHandler; for MSIE
and window.captureEvents(Event.KEYPRESS) with window.onkeypress  for netscape
to get all keypresses (and for example cancel enter unless the last field has the focus)
</body>
</html>


0
 
LVL 4

Expert Comment

by:rafistern
ID: 1273093
If you make a form with only one button then an <Enter> will submit the form:

<form action="whatever">
<input type="password" name="password" size=10>
<input type="submit" value="OK">
</form>
0
 
LVL 4

Expert Comment

by:rafistern
ID: 1273094
My solution will work in any browser, although it wasn't really what you asked.
0
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.

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 1273095
PS: A form with a single text/password field will submit with the enter in most browsers without the JavaScript:
<FORM...>
<INPUT TYPE="PASSWORD">
</FORM>

but IE3 (last time I looked) will not submit the form if there is one more entry field on the page (even if it is not in the same form)

All the 'normal' keys are passed in e.which or window.event.keyCode and can be trapped. The 13 is the enter key
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 1273096
Rafi: Stop submitting at the same time as me ;-)

Michel
0
 
LVL 4

Expert Comment

by:rafistern
ID: 1273097
Michel's last comment seems to me to be an optimal solution if it works. However I would prefer to have the button there for people who like clicking buttons - it makes the user interface more intuitive.

(I submit to the same threads as you because there seem not be many active experts on European timezones. Whatever is happening in the morning on our side of the world is the middle of the night in the States.)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

856 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