6

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.
        }
    });
});
2
  • set href="#" and remove that cursor: pointer . You're using an anchor like a div Commented Jul 3, 2013 at 11:55
  • why do you want to do that? is it you only want to register the event once? if that is the case then you can use the one event api.jquery.com/one Commented Jul 3, 2013 at 11:55

6 Answers 6

15

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

$this.html("Processing...").css({
    "cursor": "wait",
    "pointer-events": "none"
});
Sign up to request clarification or add additional context in comments.

Comments

7

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
        }
    });
});

Comments

0

$this.on("click") - this is effectively: "bind click handler to $this that will do nothing"

To trigger an event use: $this.click() or $this.trigger("customEvent")

Comments

0

May be you should use bind("click") so you can as well unbind it. e.g $("#CP").bind("click",function(){..function to run // disable the click event from CP $("#CP").unbind("click"); });. Good luck!

Comments

0
var eventhandler = function() {

    $(document).unbind("click");
    $this.html("Processing...").css({
        "text-decoration": "none",
        "cursor": "default"
    });
    $.ajax({
        type: 'post',
        url: 'abc.jsp',
        success: function(process) {
            //My code goes here...
            //Here i want to bind or add handler which is fired previously.
            $(document).click(eventhandler); 
        }
    });
}


jQuery(document).click(eventhandler);

Comments

0

Perhaps you were faced with a common problem when you’re using javaScript to submit a form, the form data is submitted multiple times if the submit button is clicked multiple times. To solve this issue you only need to write one line code with help CSS.

document.getElementById("form1").style.pointerEvents="none";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.