Solved

How to submit form when tab or enter key are pressed to change data in a text input box

Posted on 2006-07-14
7
218 Views
Last Modified: 2011-10-03
Hi,

I have a form with a text field.  I want the form to be submitted when the value is updated.  Originally I had the form submit onChange, but if the enter key is pressed instead of tab, the form does not get sumitted.  It is the only text field on the form, so it's very likely someone would enter the new quantity and press enter.

I added the onKeyDown event with this code (found here http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21362644.html), but it doesn't work....

function HitEnter()
{
     if(event.KeyCode == 13)
     {
          document.form1.action ="change_qty.cfm";
          document.form1.submit();
          nextCtrl.focus(); // simulate tab key
          event.bubleCancel = true;
          event.returnValue = false; // no more processing..
     }
}

Thanks a lot for any help,
Bonnie
0
Comment
Question by:Bonnie_K
  • 3
  • 3
7 Comments
 
LVL 23

Expert Comment

by:apresto
Comment Utility
you could use the Onchange event of the textbox

OnChange="form.submit();"
0
 
LVL 23

Assisted Solution

by:apresto
apresto earned 250 total points
Comment Utility
so your textbox might look like this:

<input type="text" name="txt" value="" onchange="form1.submit();">

then, when the value is changed in the textbox the form is submitted
0
 

Author Comment

by:Bonnie_K
Comment Utility
onChange doesn't work if you change the value and then hit enter.  

That's because by default I think the form wants to be submitted on enter, but I am not using an action element in my form tag because all submissions are done based on javascript.  So when I change the value and hit enter, the form submits to itself instead of the HitEnter() code being run.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 23

Expert Comment

by:apresto
Comment Utility
you could use the ONBlur event, this fires when the textbox will loose focus and you move to something else, i dont think its entirely necessary to wrute your own function, try iy and we'll go from there
0
 

Author Comment

by:Bonnie_K
Comment Utility
Hi apresto,

Thanks for your answers.

That didn't work either...  I think when I hit enter the form just submits, it doesn't leave the field or check to see if the value changed.

I think this is the way that forms are supposed to work, so I am thinking of giving up and just training the users to always use the tab key after changing the quantity, which does work.
0
 
LVL 19

Accepted Solution

by:
dakyd earned 250 total points
Comment Utility
Have you tried something like this?

<html>
<body>
<form onsubmit="yourStuffGoesHere(); return false;">
  <input type="text" name="key" value="value">
</form>
</body>
</html>

Forms have an onsubmit event, and you can trap it to do what you want instead.  Since hitting enter tries to submit the form, that's the same as having an event handler that traps a press of the Enter key.  Theoretically, the "return false" shouldn't be necessary since you're submitting the form somewhere, but that line will actually stop the form submission from the Enter key.  However, in case the stuff you're doing doesn't cause a redirect or the script keeps going, the "return false" will keep the form form submitting to itself.

Hope that helps.
0
 

Author Comment

by:Bonnie_K
Comment Utility
I haven't gotten a chance to get back to this project, but when I do, I will try dakyd's solution.

Thanks for the responses,
Bonnie
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now