1

I have a series of links in tabular format, all connected with different ids. I need to prevent the double clicking of them to prevent the double execution of associated server code. However the user can click different links. I say this because I have come across the following solution:

$("a.button").one("click", function() {
    $(this).click(function () { return false; });
});

Which I believe stops clicking for the whole page, once one link is clicked which is too powerful. I like the class binding approach with the above, but a user should be able to click (singularly) any link.

Thoughts appreciated. Thanks.

EDIT

Currently trying:

<a class="dblclick" href="....

and

<script type="text/javascript">
    $("a.dblclick").dblclick(function (e) { return false; });
</script> 

But does not seem to work. I am using Mozilla.

SamJolly
  • 6,347
  • 13
  • 59
  • 125
  • 1
    do you want each link to be clicked only once? – Bharath Jul 02 '14 at 16:45
  • What does "connected with different IDs" mean? – isherwood Jul 02 '14 at 16:51
  • Why is there a click inside another click? – Shaunak D Jul 02 '14 at 16:51
  • This works fine - called just once http://jsfiddle.net/HawwX/1/ – Shaunak D Jul 02 '14 at 16:56
  • Apologies. Basically I have a link like Order/Delete/1 and another link might be Order/Delete/2 and double clicking on these at present tries to double delete the relevant record. So I want to ensure a single click, and then be able to single click on another link. – SamJolly Jul 02 '14 at 16:57
  • `one` works fine, remove `$(this).click(function ()...` – Shaunak D Jul 02 '14 at 16:58
  • @isherwood thanks for the link. Must have missed it. However I am unsure how I bind an anchor tag with multiple classes, or will a.button bind to class="fa fa-check-square-o button"? Thanks you for your help as well below. Greatly appreciated. – SamJolly Jul 02 '14 at 17:24

5 Answers5

7

This would add a class to the link when it's clicked the first time. The second time, when the class is present, the link would not function.

$("a.button").on("click", function(event) {
    event.preventDefault();

    if $(this).hasClass('clicked')) { 
        return false;
    } else {
        $(this).addClass('clicked').trigger('click');
    }
});
isherwood
  • 58,414
  • 16
  • 114
  • 157
2

Just use one as you started out with.

To clarify $("a.button").one(...) will register a click exactly once per element, which seems to be be what you want.

Afterwards we only need to take care of perhaps setting the clickhandler again, when the server is done processing.

//bootstrap
$("a.button").one("click", clickhandlerOnce);

function clickhandlerOnce(event) {

    var $that = $(this),
        url = "http://www.example.com"; 

    $.getJSON(url, function(result){
        //TODO: handle your result here. 
        //NOTE: take care of errors. 

        //since we're done now with the server processing, 
        //we may bind the handler again if needed. 
        $that.one("click", clickhandlerOnce);
    });
};
Geert-Jan
  • 18,623
  • 16
  • 75
  • 137
1

Bit confused as to what you are trying to do.

Could you possibly remove the click event on the function that handles it? You can remove an event from an individual control with

$( "#foo").unbind( "click" );

So you could catch the initial click and then remove the handler.

$( "#foo" ).click(function(ev) {
  ev.preventDefault();
  $( "#foo").unbind( "click" );
});

The preventDeafault() call stops your form from submitting, just incase you are doing this within a form.

DrLazer
  • 2,805
  • 3
  • 41
  • 52
1

Try to set a flag in a element

$("a.button").on("click", function() {
    ev.preventDefault();
    if($(this).attr('data-clicked')){
        $(this).attr('data-clicked', true)
        return true
    }
    return false
});
xecgr
  • 5,095
  • 3
  • 19
  • 28
1

Try using double-click (http://api.jquery.com/dblclick/)

When you double click on a link, the click event will fire, then the double-click event. You can cancel the double-click event.

$("a.button").dblclick(function(e) { return false; });
jloosli
  • 2,461
  • 2
  • 22
  • 34
  • I have tried this, and sadly am unable to get it to work. I have added my code into a new EDIT in my question. – SamJolly Jul 02 '14 at 17:43