Solved

Enter key to respond in a text box

Posted on 1998-10-28
6
157 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
document.getElementById not worj with 2 IDs with the same name 10 40
What am I doing wrong with this Lightbox widget? 6 34
Browser Chrome downloads colored link 5 41
Jquery 10 20
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

776 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