Adrian
Adrian

Reputation: 2656

Disable href after clicking (ajax, js)

I have this code:

$('#checkout').click(function() {
    $.ajax({ 
        type: 'GET',
        url: 'index.php?route=payment/quatro/confirm',
        success: function() {
            location = '<?php echo $continue; ?>';
        }       
    });
});

What is the good way to disable this <a href> link button after clicking on it? I tried $('#checkout').disabled function, but the click function is still working.

Upvotes: 0

Views: 1610

Answers (2)

Mike Gwilt
Mike Gwilt

Reputation: 2449

If you return false; at the end of your click event, that should prevent the link from going anywhere.

$('#checkout').click(function() {
    $.ajax({ 
        type: 'GET',
        url: 'index.php?route=payment/quatro/confirm',
        success: function() {
            location = '<?php echo $continue; ?>';
        }       
    });

    return false;
});

Upvotes: 0

Adam Rackis
Adam Rackis

Reputation: 83374

Inside of your click handler I would add a call to unbind to remove the click handler going forward

$('#checkout').unbind("click");

So the full code would be:

$('#checkout').click(function() {
    $('#checkout').unbind("click");
    $.ajax({ 
        type: 'GET',
        url: 'index.php?route=payment/quatro/confirm',
        success: function() {
            location = '<?php echo $continue; ?>';
        }       
    });
});

Upvotes: 3

Related Questions