0

In my JSF page, within <script> tags I have following content.

<script>
    function func() {
        alert("Button clicked");
    }
    window.onload = function() {
       #{rich:element("btn")}.onclick = func;
    }
</script>

And I have an a4j button in page body.

<a4j:commandButton id="btn" value="save" />

After the page is loaded I click the button and it successfully displays the alert. But what I want now is to set the function func for the oncomplete event of the button. I tried in following way but didn't work. Please help.

window.onload = function() {
       #{rich:element("btn")}.oncomplete = func;
}

I prefer the answer without jQuery.

prageeth
  • 7,159
  • 7
  • 44
  • 72
  • `oncomplete` is an event for the JSF control, not DOM element – Ian Oct 29 '12 at 03:15
  • Do buttons have a (client-side) `oncomplete` event? (I don't think so.) – nnnnnn Oct 29 '12 at 03:16
  • @nnnnnn Yes `` has `oncomplete` event. when I use as `` it works. – prageeth Oct 29 '12 at 03:19
  • @ianpgall : Thanks. I understood why this doesn't work. Is there a way to accomplish this however. – prageeth Oct 29 '12 at 03:21
  • No, there is no `oncomplete` event in Javascript (as far as this context). When using an `a4j:button`, it runs an AJAX call, and then runs your code that you specify for `oncomplete`. Anything you specify for `onclick` is run before the AJAX call. As far as I know, there is no way to tap into what should be run after the AJAX call, because the code is put directly in the `` tag that is rendered. – Ian Oct 29 '12 at 03:27
  • @ianpgall : Thanks, you gave me some ideas. – prageeth Oct 29 '12 at 04:13

1 Answers1

0

I finally found a workaround.
I added the function to the oncomplete event initially, and change the body of the function at runtime.

<a4j:commandButton id="btn" value="save" oncomplete="func()"/>

Then within my <script></script> I added the following code.

<script>
     var func;
     window.onload = function() {
          func = function(){
               alert("Button clicked");
          }
     }
</script>

prageeth
  • 7,159
  • 7
  • 44
  • 72