Search code examples
jquerylivejquery-click-eventdie

reusable live click function


I have a live click function that is quite long and at one point I'm using die() on it. At another point in the code I need to re-live it.

$("#id").live("click", function(e) {
    //30+ lines of code
    if([some condition]){
        $("#id").die("click");
    }
});

function foo(){
    $("#id").live("click", function(e) {
        //Exact same code as before.. but I don't want to re-type it.
    });
}

I've tried pre-defining the function like so:

function clickEvent(e){
    //the code
}

$("#id").live("click", clickEvent(e));

but that didn't work at all.

Any help?


Solution

  • why not use .on (.live is deprecated) and use the if condition inside that instead of using .die()

        $("#id").on("click", function(e) {
            //30+ lines of code
            if([some condition]){
              //do nothing
            }
            else{
              //code that should work on click 
            }
        });