Solved

Submitting the form upon pressing enter

Posted on 2008-11-02
6
227 Views
Last Modified: 2008-11-02
Hey experts,

   I have a form with a username and password textboxes and a submit button; what I want is that upon pressing the enter key in the password field that the form gets submitted (i.e. as if the user pressed the submit button); any help on how to do that?
0
Comment
Question by:mte01
  • 2
  • 2
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
Andrej Pirman earned 200 total points
ID: 22860733
Any HTML form will by default submit when you press ENTER.

...except in case if you intentionally intercept "formname.onsubmit" event and trigger some predefined function.
0
 
LVL 20

Assisted Solution

by:chaitu chaitu
chaitu chaitu earned 150 total points
ID: 22860734
<input type="text" name="userId" onkeypress="checkEnter(this.form,event)">
<script>
function checkEnter(formObj,evt) {
  if (document.all) {
    if (event.keyCode==13) {
      formObj.submit();
    }
  }
  if (document.layers) {
    if (evt.which==13) {
      formObj.submit();
    }
  }
}
</script>
0
 
LVL 18

Expert Comment

by:Andrej Pirman
ID: 22860736
Thinking further...maybe you wanted to ask how to submit form ONLY after password is enterd, but not when username is entered?
You may go with "onsubmit" event and trigger JavaScript, which will check if both, username and password are entered, and only then do "formname.submit"
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 3

Author Comment

by:mte01
ID: 22860758
>>Labsy

Regarding your 1st comment, yeh, you're right, I am submitting the form in the js function that is put in the onClick of the Submit button (so that explains why the enter key is not working automatically), so instead I'll do this js function on the onSubmit event of the form. Your 2nd comment is not related to what I am doing.

>>chaituu

Yes, this code is good if the automatic way wasn't done; thanks for your post..
0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 22860769
mte01,
did your job is fulfilled or anything left?
0
 
LVL 3

Author Comment

by:mte01
ID: 22860818
Yes, now everything is working well with me; thanks Labsy & chaitu!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

770 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