Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Submit form on Enter

Status
Not open for further replies.

gojohnnygogogogo

Programmer
May 22, 2002
161
GB
hello,
how can I submit a form by pressing the Enter key ?

I have tried :

<SCRIPT LANGUAGE=&quot;javascript&quot;>
function enter(event,ourform) {
if (event && event.which == 13)
ourform.submit();
else
return true;}
</SCRIPT>

<form method=&quot;post&quot; name=&quot;s1&quot; action=&quot;.\SearchResults.asp&quot;>
<DIV align=center>Enter Forename<INPUT name=forename onkeypress=&quot;return enter(event,this.form)&quot;> </DIV><p>
<DIV align=center>Enter Surname<INPUT name=surname onkeypress=&quot;return enter(event,this.form)&quot;></DIV><p>
</form>

but doesn't work.
I am using WINdows2000 IIS and IE5 +
 
Usually the submit button of a form is automatically activated ('clicked') when a user presses enter. -----------------------------------------------------------------
DIM bulb
SET bulb = NEW brain

mikewolf@tst-us.com
 
problem is I don't have a submit button, just a normal button that runs some javascript to validate the page, then submit.
 
Use the &quot;onSubmit&quot; handler to run the script (returning false will keep the form from submitting). The script will execute when the form tries to submit....

<form method=post onSubmit=&quot;return myScript()&quot;> -----------------------------------------------------------------
DIM bulb
SET bulb = NEW brain

mikewolf@tst-us.com
 
In case,you havent got around the problem,try this:
function fnSubmitForm()
{
document.formname.action=&quot;define the action&quot;;
document.formname.submit();
}

//Function to submit the page on pressing the Enter key
function fnSubmitOnEnter(e)
{
var charCode = e.keyCode;
if (charCode == 13)
{
document.formname.cmdButton.focus();
}
}

And onclick of the button(cmdButton) call fnSubmitForm()
 
Forgot one little detail...

You need to add the onkeypress handler to all controls in your form as given below:

onkeypress=&quot;javascript:fnSubmitOnEnter(event)&quot;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top