Jquery live method how to disable one link, not each link

how can I disable the live ('click', function ...) method and then after the ajax call (depending on the response) then enable it again?

I read something that "event.preventDefault ();" Don't work with the live method, and even if it works, how can I enable it again? I cannot use the die ('click', function ...) method because it will disable all links, not just the one link I clicked on.

+2


source to share


1 answer


lets say you have this code

$('a.clickers').live('click', function() {
    ///...
});

      

what you would like to do is highlight the items on click. Handler functions in jquery always have their context assigned to the element they are processing at the time (i.e. this anchor element you clicked on). So we have to use it and call it individually like this.



$('a.clickers').live('click', function clickHandler() {
    // keep a reference to the link that is clicked on so we can refer to it
    // later in the ajax handler.
    var elementClickedOn = this;
    // removes the live event handler
    // from just this link
    $(elementClickedOn).die('click', clickHandler);
    // your code
    // ajax call, im not 100% familiar with ajax in jquery
    // but you get the gist.
    $.ajax(server, function ajaxHandler(responseargs) {
        if (responseargs.reEnableConditionMet) {
            //renable the element live event handler, by referring to the 
            //original function
            $(elementClickedOn).live('click', clickHandler);
        }
    });
});

      

I hope this is close to what you were looking for.

+3


source







All Articles