Welcome to WebmasterWorld Guest from 54.163.34.237

Forum Moderators: incrediBILL

Message Too Old, No Replies

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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 4, 2002
posts:2204
votes: 0


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)

Senior Member

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

joined:Feb 25, 2002
posts:3185
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 20, 2004
posts:1475
votes: 0


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)

Senior Member

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

joined:Feb 25, 2002
posts:3185
votes: 0


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)

Senior Member

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

joined:Mar 2, 2003
posts:3710
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 4, 2002
posts:2204
votes: 0


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)

Senior Member

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

joined:Mar 2, 2003
posts:3710
votes: 0


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.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members