Skip to main content
February 2, 2012
Answered

How to set default button when press "Enter"

  • February 2, 2012
  • 1 reply
  • 1457 views

Hi,

I have a form and it has only one button (Submit). When I press "Enter", it can submit the form and do the action. However, when I add another button (Normal button, not submit), it cannot perform submit action directly when I press "Enter".

If we press "Enter" and the form can submit automatically, it is more user-friendly than without this feature. Therefore, may I know the method of set the default button when users press "Enter"?

    This topic has been closed for replies.
    Correct answer Dan_Bracuk

    At first I thought the order of the form controls might matter.  When I run this code:

    <cfdump var="#GetTickCount()#">
    <form action="TestCfgrid.cfm" method="post">
    <input name="x" type="text" />
    <input type="button" value="x" />
    <input type="submit" />

    </form>

    While I am in the text box, if I press the enter key the form submits.  I originally tried it with the submit and button inputs in the opposite order.

    Note that the enter button will not submit the form if you are in a select control (aka dropdown) or a textarea or if your focus is not on a form element.  That's the default behaviour.  If you want to override that, you'll have to write some javascript.

    1 reply

    Inspiring
    February 2, 2012

    What does the other button do?

    February 2, 2012

    The new button is used to call the cfc for opening a new sub-window. After some data selection, it sets the result to the form field. Therefore, the button is just set as a normal button. No Submit or Reset will be performed by clicking this button.

    Dan_BracukCorrect answer
    Inspiring
    February 2, 2012

    At first I thought the order of the form controls might matter.  When I run this code:

    <cfdump var="#GetTickCount()#">
    <form action="TestCfgrid.cfm" method="post">
    <input name="x" type="text" />
    <input type="button" value="x" />
    <input type="submit" />

    </form>

    While I am in the text box, if I press the enter key the form submits.  I originally tried it with the submit and button inputs in the opposite order.

    Note that the enter button will not submit the form if you are in a select control (aka dropdown) or a textarea or if your focus is not on a form element.  That's the default behaviour.  If you want to override that, you'll have to write some javascript.