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
220 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
ID: 17109160
you could use the Onchange event of the textbox

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

Assisted Solution

by:apresto
apresto earned 250 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Opening PDF on button click and fill new document 2 38
If condition on Html with Asp 11 17
Index on a Table 6 15
Jquery 2 12
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

777 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