Enter Key Press behave like Submit in JSF

17,337

Solution 1

I haven't seen this issue before. The chance is little that this behaviour is browser specific. Try in different kinds of browsers to exclude the one and other (IE6/7/8, FF, Safari, Chrome, etc). The chance is bigger that this is caused by a (poor) Javascript key event listener which incorrectly suppresses the enter key (for example a JS password validator which checks the entered characters).

If still in vain, just add the following onkeypress to the h:form:

<h:form onkeypress="if (event.keyCode == 13) this.submit();">

You need to take textareas into account however. If you have them, then you need to copy all onkeypress events over the h:inputXXX elements expect of textareas yourself.

Solution 2

If you want to press ENTER key instead of submit in any form, what we have to do here is add defaultcommand attribute in af:form tag and give id of the submit button as value. Sample code snippet for this is

<af:form id="f1" defaultCommand="cb1">
<af:outputText value="User Name" id="usename"/>
          <af:inputText value="#{BackingBean.userName}" id="uname" />
          <af:outputText value="Password" id="pword"/>
          <af:inputText value="#{BackingBean.password}" id="paword" secret="true"/>
          <af:commandButton text="Submit" action="#{BackingBean.method}" id="cb1" />
</af:form> 
Share:
17,337
ComputerPilot
Author by

ComputerPilot

Updated on June 04, 2022

Comments

  • ComputerPilot
    ComputerPilot almost 2 years

    How to make Enter Key Press behave like Submit in JSF. It works with InputBoxes; but not with inputSecret boxes

  • matbrgz
    matbrgz about 14 years
    It is browser specific, especially with multiple forms on the same page.
  • BalusC
    BalusC about 14 years
    @Thorbjorn: "it works with inputboxes, but not with inputsecret boxes" and I expect that they are in this particular case in the same form. Multiple forms should be no problem, knowing the fact that the enter key would only submit the parent form. Further there's an IE bug which wouldn't make the parent form to submit if there's only one input element. But likely none is the case here.
  • Fenton
    Fenton about 14 years
    With textarea (as opposed to an input type of text), bear in mind that people think you can hit enter to get a new line - do they really want the form to submit?
  • Fenton
    Fenton about 14 years
    not quite. You covered how to add the event to textareas, but didn't point out that the decision needed to be made as to whether that's a good idea (generally, it's a bad idea).