Submit a form by pressing 'Enter'
MatthewHSE
Status: Contributor
Joined: 20 Jul 2004
Posts: 122
Location: Central Illinois, typically glued to a computer screen
Reply Quote
Yes, I know that's the default behavior! ;) But not when you don't have an action attribute set in your <form> tag; in that case you can apparently hit 'Enter' as much as you want and it won't submit, probably because it doesn't know how.

I have a form that I needs to be submitted with Ajax. From the reading I've done so far, this means removing the action attribute from the <form> tag, and adding a javascript function to submit the form to a serverside script when the 'Submit' button is clicked. So far so good, assuming the visitor actually clicks the button instead of hitting 'Enter,' which is what I normally do.

So I guess the question is actually fairly simple. How can I attach a javascript function to the 'Enter' key, that will ONLY take effect when the key is pressed with one of the form fields focused? Seems like it should be easy, but for the life of me I can't find the answer.
Back to top
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 4127
Location: East Coast, West Coast? I know it's one of them.
Reply Quote
this isn't clear. If you need the enter button to be clicked, then the enter button has focus. So I think you probably mean something else than what you said. Or do you mean the enter button on the keyboard?

Focus refers to the active field in terms of your mouse, more or less. Something has focus when your mouse, or cursor, is over or in it.

Do you mean that one of the fields has been completed?

You don't actually need the submit button by the way, you can have the form submit itself if you want. Or not. There's lots of ways to do it.

You can add an 'onclick' handler, I think that works on <input .. submit> buttons, can't remember for sure.

Here's a function from another site, this one is triggered by an onclick event in a link, you can actually make a link in the form, and then use an image of the submit button, that's another option, that would work.

:: Code ::
function addToCart(product_id)
{
   document.addCart.products_id.value = product_id;
   document.addCart.submit();
   return false;
}


Notice the 'return false', that makes the action not do anything. Actually, in this case, I think if you wanted the button to be a real submit/enter button, you'd make it 'onsubmit', not 'onclick', I can't remember, with the action set to standard, whatever you want it to be, and the method what you want it to be.

However, without having more details it's not possible to go further.

<added>Oh, wait, you do mean the keyboard 'enter' button, no? So the user fills out a field, then hits enter? For programming stuff, you have to give more verbs and descriptions or it's too hard to figure out what is actually required.

Here's one:
:: Code ::
<script type="text/javascript">
function send()
{document.theform.submit()}
</script>

<form name="theform" method="post"
action="/some/action/or/other" enctype="text/plain">

<b>Submit your Name:</b>

<input type="text" name="fred" size="30"
maxlength="30" onUnfocus="send()">

</form>


Note however, that as usual with scripts I find at random, this one has an error in it, onunfocus means that the form submits when the user moves their mouse or cursor away from the form field. If the below is correct, and if you have only a single form field [details, give details, there is no such thing as a generic programming problem], you don't need anything, just hit enter and the action will take place.

My guess is that the 'onsubmit=some_function();' event handler will do the trick however.

Info on html, forms, and enter here
:: Quote ::
This was evidently meant as a convenient way to submit simple queries, but reducing the risk, on a complex form, of prematurely submitting it while trying to fill it in. Numerous browser implementers (e.g Netscape, Opera, various versions of Lynx,...) followed this advice, though it seems with slightly different interpretations. If you use only one "single-line text input field" (i.e input type="text"), along with other kinds of form widget such as buttons, selections etc., then these browsers will submit the form. Some (e.g Netscape) did this even if the form also contained multi-line input field(s) i.e textarea element(s), but some others will not. Therefore, for widest accessibility of Enter=>Submit, you would want to use no textarea elements in the same form.

Back to top
MatthewHSE
Status: Contributor
Joined: 20 Jul 2004
Posts: 122
Location: Central Illinois, typically glued to a computer screen
Reply Quote
Sorry, sometimes I get so wrapped up in what I'm doing that I forget important details. ;) (More info here.)

A form with the action attribute set will submit at any time by pressing the "Enter" key on the keyboard, provided one of the form fields has focus. (Which I always took to mean the text cursor was in it or it was otherwise selected; that's accurate, isn't it?) A form without the action attribute set will not submit by pressing the keyboard "Enter" key. (That is, it seems to submit, but really just reloads the page.)

The form I'm working with has the "Submit" button set to type="button" instead of type="submit". With the submit type, the form (without an action attribute) will still submit when the "Enter" key is pressed, but just reloads the same page. With the button type, the form doesn't submit at all when "Enter" is pressed on the keyboard.

I've been submitting the form with an onclick event handler attached to the form button, which runs a JS function to submit the form. That works provided someone actually clicks the button, which I guess most will do, but I'd like to be able to make the keyboard "Enter" key run the same JS function that clicking the button does. That way, people can still submit the form with the mouse or the keyboard.

Hope this clears some things up - the other thread should help too. In fact, all this probably belongs in the other thread anyway. Sorry for the confusion.
Back to top
techAdmin
Status: Site Admin
Joined: 26 Sep 2003
Posts: 4127
Location: East Coast, West Coast? I know it's one of them.
Reply Quote
See this thread for solution to this. At least a tentative solution.

Programming is a weird beast, what makes sense to you makes no sense to anyone else. Or you, 6 months from now, which is why commenting and being able to explain in relatively clear english what is happening is important.

I know what you mean about getting wrapped up, I have a navigation script that is so complex that it takes me usually a few hours to just figure out how it runs when I want to implement it again. Very bad job on my part, but enormously powerful.

Anyway, with the other script on the other thread, you just have the 'onclick' on the button, and the 'onsubmit' on the form. I think that will work, since in either case the function will get called. Try and let us know.
Back to top
Display posts from previous:   

All times are GMT - 8 Hours