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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

default buttons on forms

Status
Not open for further replies.

marketingmaggie

Technical User
Jul 13, 2001
10
GB
Does anyone know how to make the enter key action the submission of a form, instead of having to click on the button? My little form is as follows, as I'd thought that adding the reset button would force the submit button to become the default for the enter key. But it doesn't! Just nothing happens when I press the enter key.

<form name=&quot;f&quot;>
<input type=&quot;password&quot; name=&quot;pwd1&quot; size=&quot;25&quot;>
 <br>
<input type=&quot;button&quot; name=&quot;btn1&quot; value=&quot;enter&quot; onclick=&quot;validate()&quot;> 
<input type=&quot;reset&quot; value=&quot;reset&quot; name=&quot;B1&quot;>
</form>

Many thanks: it's always these simple things which are so hard to find out.

MarketingMaggie
 
Corey,

Thanks for responding. I've changed the button type to &quot;submit&quot; as below:
<form name=&quot;f&quot;>
<input type=&quot;password&quot; name=&quot;pwd1&quot; size=&quot;25&quot;>
&nbsp;<br>
<input type=&quot;submit&quot; name=&quot;btn1&quot; value=&quot;enter&quot; onclick=&quot;validate()&quot;>
</form>
but I still need to click (or double-click) the button to make the form move to validation - which is a separate bit of javascript and works fine. What I want is to be able to validate by depressing the enter key instead - well, either/or would be good. But using the keyboard just doesn't produce an effect other than to clear the passowrd box. Where now?

Maggie
 
Corey,

This is the javascript. Clicking the button works fine, calls up the &quot;home&quot; page. But you'll have to set the window.location to something on your own machine, as this is not a live site yet.

<script type=&quot;text/javascript&quot;>
<!--
function validate(){
var entry=document.f.pwd1.value.toLowerCase();
if(entry==&quot;mosque&quot;) window.location=&quot;else{
alert(&quot;password incorrect - please re-try&quot;);
document.f.pwd1.value=&quot;&quot;;}
}
//-->
</script>
Maggie
 
This seemed to work for me:
Code:
<script type=&quot;text/javascript&quot;>
<!--
function validate(){
var entry=document.f.pwd1.value.toLowerCase();
if(entry==&quot;mosque&quot;) window.location=&quot;[URL unfurl="true"]http://oemcomputer/ISRSA2004/pass/home.htm&quot;;;[/URL]
else{
alert(&quot;password incorrect - please re-try&quot;);
document.f.pwd1.value=&quot;&quot;;}
}
//-->
</script>
</head>

<body>
<form name=&quot;f&quot; method=&quot;post&quot;>
<input type=&quot;password&quot; name=&quot;pwd1&quot; size=&quot;25&quot;>
 <br>
<input type=&quot;submit&quot; name=&quot;btn1&quot; value=&quot;enter&quot; onclick=&quot;validate()&quot;>  
<input type=&quot;reset&quot; value=&quot;reset&quot; name=&quot;B1&quot;>
</form>


__________________________
Corey

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top