1

I have a jsf form which has a command Button and i am going to disable it when user clicked on it to prevent resubmit the form.

Here is commandButton code:

<h:commandButton id="loginbtn" value="Sign in" action="#{loginBean.login()}"class="btn-myForm" onclick="myFunction()">
<f:ajax execute="@form" render="@form" />
</h:commandButton>

And here is myFunction() script in jsf form:

<script>
  function myFunction() {
     document.getElementById("loginbtn").disabled = true;
 }
</script>

But myFunction() not triggered when i click on button.

  • Add a `console.log` to `myFunction` to see if it is really not triggered. I guess it gets triggered, but as the `loginbtn` is inside a `h:form` the id of the button in the DOM is not `loginbtn` but rather `formname:loginbtn` or something like that. Try to use that ID instead inside `myFunction` with `formname` replaced with the name of your form. – user1983983 Mar 12 '15 at 10:33
  • @user1983983 What should be in `console.log()` argument? –  Mar 12 '15 at 12:11
  • 1
    Any String and then check the console for that String. If it is logged the function is called, if not it is not. – user1983983 Mar 12 '15 at 12:23
  • 1
    I can see only one thing in this case. Pay close attention here `action="#{loginBean.login()}"class="btn-myForm"`. There is a missing space between these two attributes `action` and `class` associated with the said `` but that should give you a parse error on the server side. Besides this, this problem cannot be reproduced on a blank playground project having a single XHTML file. Therefore, you should be explicit on your side stating anything that may be arriving from external environment. – Tiny Mar 12 '15 at 12:35
  • Even if your script did work, your action will not be executed because the runtime will ignore all `disabled="true"` elements. What you really want is to [overlay a panel on the button or wait until just after the button has sent its data to the server-side](http://stackoverflow.com/questions/11744597/how-can-i-disable-an-hcommandbutton-without-preventing-the-action-and-actionlis). The problem here is that `onclick` is triggered immediately after the button is depressed, disabling the control before data submission is complete – kolossus Mar 12 '15 at 16:08
  • @Tiny Sometimes, something is going wrong on the server side. You are right, it should give a parse error, but only theoretical. :) – alexander Mar 13 '15 at 20:18

0 Answers0