views:

968

answers:

4

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

+2  A: 

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.

BalusC
It is browser specific, especially with multiple forms on the same page.
Thorbjørn Ravn Andersen
@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.
BalusC
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?
Sohnee
@Sohnee: uh, I already covered that in my answer?
BalusC
@BalusC - 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).
Sohnee
@Sohnee: uh, it's nothing more than obvious :)
BalusC
A: 

I tried it with Chrome and It worked perfectly. Any suggestions to make it work in IE? I have only one form and one input element(InputSecret box) - As BaluC said it could be because of the "IE bug which wouldn't make the parent form to submit if there's only one input element"

I tried the onkeypress event of the form to make it work in IE and it's not working. Something is happening even before the onkeypress event of the form is called?

I tried adding an additional hiddenBox and it did not work either. But adding an additional inputBox or InputSecret box did make it work in IE. How to make it work without adding any additional visible input elements?

ComputerPilot
A: 

Hi All I made it work by placing an additional inputBox and hiding it using javascript. Let me know if you have any other suggestions Thanks baluC for pointing me in the right direction Jerry

ComputerPilot
A: 

There is an old specification that pops into my mind with this one. If you have a form that contains just ONE input field, the behaviour of submitting on the enter-key doesn't work in some versions of Internet Explorer. The easiest fix is to make sure you have more than one input field.

Other reasons for this problem include...

  • Not having an input of type submit
  • Having an input of type submit, but it isn't visible on the page (hidden or positioned off-page)

This behaviour is very specific to this browser.

MS Bug Report Here:

https://connect.microsoft.com/IE/feedback/details/389736/pb18344-submit-button-value-not-posted-with-form-submission

Sohnee