views:

77

answers:

2

I have a big function defined and executed on an element click. Is there a way to execute again that "big" function later, call it somehow, without writing it again?

$('#element').click(function(big){
    some big function
    ...
    ...
});


$('#another_element').click(function(){
    this should execute the previous "big" function
});

Thanx.

+4  A: 

If you define your function external to the call you can use it as many times as you like.

var bigfunction = function(big) { ... };

$('#element').click(bigfunction);
$('#another_element').click(bigfunction);

You can also declare it the usual way:

function bigfunction(big) { ... };
tadman
Thanx, it seems to work but now I have a new problem, I need to add an if(clicking == false) return; , I've tried to add it to the var function but it does not work, can I somehow add it to the click?Thanx again
Mircea
As far as I know, that should work. It probably depends on what the variable clicking refers to as it might be outside the scope of the function when it executes.
tadman
+3  A: 

You should make the "big function" a separated function...

function the_big_function(){
   // do stuff
}

$('#element').click(function(big){
   the_big_function();
});


$('#another_element').click(function(){
    the_big_function();
});
j.