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!

How can I set any button of the form as default? 1

Status
Not open for further replies.

rejoice

MIS
Jun 5, 2002
42
MY
Hi, everybody.

I have a form with 2 buttons (Back and Next), I wish to make the Next button as default (means when user press Enter key, this button will be clicked).

Is there any way I can do it?
 
Hi rejoice,

You can set the focus to the specific button. (see the function with "unload"). Then when you hit enter, it's like the the button is pressed.

<HTML>
<HEAD>
<TITLE></TITLE>

<script language=&quot;javascript&quot;>
function thatfocus()
{

document.myform.xxxx.focus();

}

</script>
</HEAD>

<BODY onload=&quot;thatfocus()&quot;>
<FORM name=&quot;myform&quot;>
<INPUT type=&quot;text&quot; name=&quot;field1&quot; class=&quot;readonly&quot;><br>
<INPUT type=&quot;text&quot; name=&quot;field2&quot; class=&quot;NOTreadonly&quot;><br>
<INPUT type=&quot;text&quot; name=&quot;field3&quot;><br>
<input name=&quot;xxxx&quot; type=&quot;submit&quot;>
</FORM>
</BODY>
</HTML>

Hope this helps,
Erik <-- My sport: Boomerang throwing !!
!! Many Happy Returns !! -->
 
Thanks for your inputs, Boomerang.

I have tried this program. The program set the button focus when user first get this form, but when the user input something in the text field and press ENTER key, the button does not selected automatically.

I know nothing about javascript, but I do hope javascript can do this. may be you can give me some idea.

Thanks.
 
Hi Rejoice,

Did you ever get this figured out? I have this problem as well.

Cheers,

Nnick
 
Instead of using Javascript I just place my SUBMIT (any button) button on the left most of other buttons. It will be click whenever user press ENTER key.
 
i though the default action was to submit the form if there was only one form on the page when the enter key is pressed?
 
Pressing [ENTER] performs the action specified in the form tag -- regardless of which button has focus.

If a button has focus, pressing [SPACE] executes the click of that button.

So, if, when the user presses the [ENTER] key, you want to execute the onclick action of &quot;Next&quot;, then this will be your submit button for that form:
Code:
<input type=&quot;submit&quot; value=&quot;Next&quot;></input>

Hope that helps.

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
What Edward said, and:

Have your (default) button as a submit button, and your 'back' button as a normal button.

Code:
<input type=&quot;button&quot; value=&quot;Back&quot; onclick=&quot;this.form.submit();&quot;></input>
<input type=&quot;submit&quot; value=&quot;Next&quot;></input>

If that's what you are trying to achieve. If you're still stuck you'll have to post what you are using at the moment.



Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
I was researching this myself and came up with this option for handling this problem. By capturing the keydown event you can then simulate the actual pressing of the submit button. I used this way of doing the submit in order to prevent the necessity of changing code on subsequent pages.

<script LANGUAGE='JAVASCRIPT'>
<!--

ns4 = (document.layers)? true:false
ie4 = (document.all)? true:false

function keyDown(e)
{
var nKey
if (ns4) {nKey=e.which;}
if (ie4) {nKey=event.keyCode;}

if (nKey=='13') // enter key
{
document.forms[0].cmdSubmit.focus();
document.forms[0].submit
}
}

document.onkeydown = keyDown
if (ns4) document.captureEvents(Event.KEYDOWN)
//-->
</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top