Solved

Submitting the form upon pressing enter

Posted on 2008-11-02
6
230 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
[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
  • 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

696 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