jQuery in Action, 2nd edition*
The moose likes JSF and the fly likes Check to see if a button is clicked once or twice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Check to see if a button is clicked once or twice" Watch "Check to see if a button is clicked once or twice" New topic
Author

Check to see if a button is clicked once or twice

Deniz Atak
Ranch Hand

Joined: Jun 29, 2011
Posts: 30
I have a button which I want to test if it is clicked once or twice:



The output of this code is:



It seems that the buttons attribute never changes. Does anyone have any opinion about this issue?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16020
    
  20

If you're looking for double-click handling, that's not possible in server code because the UI events are all saved up and sent in a batch request. On the client side, double-click handling is a basic JavaScript event.

If you're looking for a way to avoid the infamous "Please don't click this button more than once or you may be billed multiple times" situation, that's different.

The problem with multiple submits of a form is that each time you click the submit button, a separate asynchronous request is sent to the server, which will then pass it on to a server thread that can run simultaneously with the original submit.

To handle stuff like that, I recommend giving each critical form some sort of unique transaction ID that gets submitted with the form. A hidden field will do. The server-side code should then incorporate a synchronization mechanism that inspects that ID and bounces, ignores, or otherwise safely handles the duplicate submissions. Don't use the transaction ID for accessing anything secure, however, because it can be easily hacked. Just use it as a safeguard.


Customer surveys are for companies who didn't pay proper attention to begin with.
Deniz Atak
Ranch Hand

Joined: Jun 29, 2011
Posts: 30
Hi Tim,

first, thanks a lot for your answer. Now I understood that I was in the wrong way.

I tried double-click handling on the client-side with the following code:
js:

jsf:


I am trying to change the name of the button when it is clicked once. But it is not working. Do you have any opinion about why this is not working?
I am tring the application in Mozilla Firefox.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16020
    
  20

I don't know what you mean by "name of the button". HTML buttons have a "name=" attribute, but JSF buttons do not. Changing the ID attribute wouldn't help, and changing the action= EL expression likewise - since the EL has been compiled into something incomprehensible by the HTML renderer.

If you want specifically to ignore a double-click, set an ondblclick="return false" to suppress the submit operation. Clicking twice at intervals exceeding the double-click interval doesn't get covered by that, however.

You can also disable the button via javascript once it has been clicked.

Whether a browser will fire both click and double-click events on the same operation, I don't actually know. That would make a difference in how to handle this, though.
Deniz Atak
Ranch Hand

Joined: Jun 29, 2011
Posts: 30
Hi Tim,

sorry I was a little bit tired when writing an answer. What I meant by "name of the button" was the text on the button. I want to click on the button, select all checkboxes in the table below it and click on it again and unselect the checkboxes. Meanwhile when clicked on once I want to change the label of the button from Select All to Unselect All. But

seems not working. Do you have any ideas why this is not working?
Thanks in advance.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16020
    
  20

I see 2 possible problems.

First, there's the JavaScript code. What you did MAY work, but the way I'm used to doing it is more like:


The other likely problem is the ID you're using. When the element is a JSF-constructed object, its actual HTML ID is usually going to be more like "form1:selectAllButton", where "form1" is the ID of the naming container, assuming you had an "id="form1"" on your form. And if you didn't provide the containing form with an ID, one will be made up more or less randomly.
Deniz Atak
Ranch Hand

Joined: Jun 29, 2011
Posts: 30
Hi Tim,

thanks again for your answer.

I tried:

but it is still not working. I even tried the exact path of the button:

it did not worked either.

I also tried this:

but it gave no results, alert box did even not show up.

It looks like I can not reach the button element at all. Can this be because of Woodstock component library? I mean the button is from this library, maybe I can not reach its elements via JS?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16020
    
  20

Deniz Atak wrote:
It looks like I can not reach the button element at all. Can this be because of Woodstock component library? I mean the button is from this library, maybe I can not reach its elements via JS?


It's possible. I don't know what the generated HTML looks like.

On a straight h:commandButton, I believe that this works:
Deniz Atak
Ranch Hand

Joined: Jun 29, 2011
Posts: 30
Hi Tim,

I found the problem: I had to write:


instead of:


but now there is another problem :
when I clicked on the button, the value of the button changes but immediately after that the page is re-rendendered and the button looses its value, returns to the old state..
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16020
    
  20

Deniz Atak wrote:Hi Tim,

I found the problem: I had to write:


instead of:


but now there is another problem :
when I clicked on the button, the value of the button changes but immediately after that the page is re-rendendered and the button looses its value, returns to the old state..


In that case, forget the javaScript and set the label as a property in the backing bean. Have the action method on the button update the property value so that it will display when the control re-renders.
Deniz Atak
Ranch Hand

Joined: Jun 29, 2011
Posts: 30
it has its own problems.. but I will give it a try again ..
Thanks for your kind answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Check to see if a button is clicked once or twice