Diganta
Diganta

Reputation: 691

<p:commandButton>'s action listener not firing with attribute type="button"

Is it possible to fire action,actionListener of a <p:commandButton> with attribute type="button". I have a requirement where in a form there are text boxes and command buttons. If I press enter in any text box then commandButton is invoked. So I change all command buttons type to "button". Now problem I am facing that command button's action, actionListener not firing.I want to solve it with out using java script. Also I read this This Link. Can any one tell me where I should edit or change to get expected output. Thanks.

Upvotes: 1

Views: 1145

Answers (1)

BalusC
BalusC

Reputation: 1108537

Using type="button" is the wrong solution to prevent enter key from submitting the form. It basically changes the submit button to a dead button which does not submit the form and is only useful for attaching JavaScript onclick and likes. You're simply facing the consequences of this wrong solution. You should not try to fix it, but take a step back and solve the initial problem the right way.

One of the ways is:

<h:form onkeydown="return event.keyCode != 13">

Or, more generically, with jQuery, which skips textareas from the restriction:

$(document).on("keydown", ":input:not[textarea]", function(event) {
    return event.keyCode != 13;
});

Note: jQuery is already bundled in PrimeFaces, you do not need to install any scripts separately. Also note that you really can't go around JavaScript here. Even more, PrimeFaces/ajax components rely on JavaScript and wouldn't work anyway without JavaScript.

Upvotes: 4

Related Questions