Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable/Disable Click event for element using jQuery

Tags:

jquery

events

I want Enable/Disable click event on element. I have following code...

HTML:

<a id="CP" style="cursor: pointer; text-decoration: underline;">Current Processes</a>

jQuery:

$(document).on("click", "#CP", function(event) {
    $this = $(this);
    $this.click(function() {
        return false;
    });
    $this.html("Processing...").css({
        "text-decoration": "none",
        "cursor": "default"
    });
    $.ajax({
        type: 'post',
        url: 'abc.jsp',
        success: function(process) {
            //My code goes here...
            $this.on("click"); //  Here i want to bind or add handler which is fired previously.
        }
    });
});
like image 707
Rickyrock Avatar asked Jul 03 '13 11:07

Rickyrock


2 Answers

If I understand well, you could do that very simple with the following:

$this.html("Processing...").css({
    "cursor": "wait",
    "pointer-events": "none"
});
like image 116
Jeroen Bellemans Avatar answered Nov 15 '22 07:11

Jeroen Bellemans


Add some class when performing AJAX request. Remove when done. Do nothing if link has this class.

$(document).on("click", "#CP", function(event) {
    event.preventDefault();
    $a = $(this);
    if($a.hasClass('disabled')) {
        return false;
    }

    $a.html("Processing...").addClass('disabled');
    $.ajax({
        type: 'post',
        url: 'abc.jsp',
        success: function(process) {
            $a.removeClass('disabled');

            // restore inner HTML here
        }
    });
});
like image 43
Ruslanas Balčiūnas Avatar answered Nov 15 '22 06:11

Ruslanas Balčiūnas