1 Reply Latest reply on Sep 19, 2007 1:58 PM by sergeysmirnov

    a4j:commandButton / a4j:commandLink 'onclick' attribute

    alonsodomin

      Hi there,

      We're using richfaces 3.1 to develop a simple webapp and I would like to add a javascript "confirm" function to a commandLink which deletes a current entry from a database so users have the chance for cancel the action just before it is performed.

      My commandLink is inside a rich:dataTable and there is one per row. my commandLink is as follows:

      <a4j:commandLink actionListener="#{Customer.delete}"
       onclick="return window.confirm('Are you sure?');"
       ....
      >
      ...
      </a4j:commandLink>
      


      When users click on the rendered link, a confirm box is displayed but no action is performed when users clicks the "ok" button.

      If I use a <h:commandLink> rather than an <a4j:commandLink> the action is performed when user clicks the "ok" button.

      The reason for the post is <a4j:commandLink> doesn't renders the 'onclick' attribute the same way the <h:commandLink> does: This last one generates two javascript functions: "a()" and "b()", the first one holds the value of the 'onclick' attribute, the last one holds the code that submits the page; so, if "a()" returns true (the user clicks the "ok" button), the page is submitted, otherwise, nothing happens.

      I want to use the <a4j:commandLink> because I don't want to reload the entire page, I just want to rerender the dataTable.

      Is this a bug? Is there any workaround?

      Regards,
      Alonso

        • 1. Re: a4j:commandButton / a4j:commandLink 'onclick' attribute

          You confuse two different things.

          onclick="a()" where function a() returns false
          and
          onclick="return a()"
          are not the same things.

          First Example:

          <div onclick="window.confirm('sure?');alert('you are sure!')" >Click Me</div>


          The alert is shown always. Does not matter, what you click in the confirmation box.

          Second example:

          <div onclick="return window.confirm('sure?');alert('you are sure!')" >Click Me</div>


          The alert is not shown. Never. Just because, you say return in the first statement. Does not matter, what is returned. If JS interpretor sees a return, it stops the function and returns.

          What you have in your case, is an equivalent of the second example. RichFaces puts your code in front of the ajax call. It tries to be hospitable :-)

          What to do with the second example to make it workable like it is expected?

          Try:

          <div onclick="if (! window.confirm('sure?')) { return false }; alert('you are sure!')" >Click Me</div>