Welcome to WebmasterWorld Guest from 54.163.84.199

Forum Moderators: incrediBILL

Form submit on enter

OK its an easy one - but it is friday and I want to home

   
4:00 pm on Oct 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Quick question as my brain has taken leave of it's memory. How do I get a form to submit when a user hits enter, not just by clicking the button.

ta, Limbo

4:16 pm on Oct 1, 2004 (gmt 0)

WebmasterWorld Senior Member blobfisk is a WebmasterWorld Top Contributor of All Time 10+ Year Member



How about using onkeypress on the input fields:


function submitMe() {
if (window.event.keyCode == 13)
{
document.myForm.submit();
}
}

Then in your HTML:


<input type="text" ... onKeyPress="submitMe()">

This is off the top of my head and may be way off!

HTH

4:20 pm on Oct 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Depending on how complex your form is, it should be able to be submitted by the user pressing "Enter" while focus is on any text field, by default.

You should only need to put a script to trigger the submit function in fields which are not of the type="text".

4:24 pm on Oct 1, 2004 (gmt 0)

WebmasterWorld Senior Member blobfisk is a WebmasterWorld Top Contributor of All Time 10+ Year Member



The behaviour of browsers should be that if the form only has one input then hitting return should submit the form. However, behaviour varies widely between the browsers!
11:09 pm on Oct 1, 2004 (gmt 0)

WebmasterWorld Senior Member kaled is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I'm half asleep as I write this, but I think it is as simple as setting type="submit".

I wrote a demo page some months ago (with three forms on a single page) and I think I tested it on IE, FF and Opera and all behaved correctly.

Kaled.

10:09 am on Oct 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Whoops

I do apologise, forgot to say thanks for the replies.

Unfortunately none of the suggestions seemed to work. I have aquired the page from another.

Code:

<form name="frm">
Enter password
<input type="password" name="pswd" size="6">
<input name="button" type="button" onsubmit="loadpage(target)" onClick="loadpage(target)" value="Proceed">
</form>

Thanks, Limbo.

2:03 pm on Oct 8, 2004 (gmt 0)

WebmasterWorld Senior Member kaled is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Change type="button" to type="submit" and delete the OnClick value.

Finally, set <FORM OnSubmit="return processForm();"> where processForm is the name of your validation function. It must return true to permit form submission to proceed.

Kaled.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month