Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
229 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
[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
  • 3
  • 3
7 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 17109160
you could use the Onchange event of the textbox

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

Assisted Solution

by:apresto
apresto earned 1000 total points
ID: 17109176
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
ID: 17110592
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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 23

Expert Comment

by:apresto
ID: 17112152
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
ID: 17112304
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 1000 total points
ID: 17125604
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
ID: 17204282
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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…
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…

722 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