Link to home
Start Free TrialLog in
Avatar of Steven_Inter
Steven_Inter

asked on

Default Submit Button upon clicking the Enter Key


I have two textboxes and two submit buttons inside a form .  The two textboxes are single line input fields.  Nothing fancy.  The two submit buttons take you to two different places.  The first button is a "Back" button, the Second button is a "Save" button.  I want the default button to be the second button, the Save Button.  But I do not want to change the order of the Buttons.

I have searched the internet a fair bit with no luck.  Some solutions I found were creating a hidden submit button that mirrored the "Save" button and placing this as the first button in the form.  Don't like this idea.
Looking for a simple and easy to follow solution as many people will be using this code.
ASKER CERTIFIED SOLUTION
Avatar of hongjun
hongjun
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just make the first one type="button" instead of type="submit"
Hi,

If end users manually clicking the buttons, then no issues. When they keep pressing tabs then we can set the tabindexes for each elements the in the required order.

And another alternative is ....
<script language="javascript">
           function chFocus(){
      document.forms[0].btnSave.focus();
          }
</script>
<form>
<input type="text" name="txtObj1" tabindex="1" value="" onchange="chFocus();">
<input type="text" name="txtObj1" tabindex="2" value="" onchange="chFocus();">
<input type="submit" name="btnBack" value="BACK" tabindex="4">
<input type="submit" name="btnSave" value="SAVE" tabindex="3">
</form>

Cheers,
SRS
Like kiddanger says, make your non-default buttons type="button", and your default button a type="submit".  The way HTML works is the type="submit" button will alsways be 'clicked' when enter is pressed; IF it is the only "submit" button on the page.

Barry62
Avatar of Steven_Inter
Steven_Inter

ASKER

Thanks everyone for your tips and advice.  Much appreciated.