jquery - Why does click event handler fire immediately upon page load? -


i playing around function want bind links. @ present function fires when page loads, instead of when click on link.

here's code. (i can past in function showdiv() if need see it.) can tell if i'm doing wrong or stupid here?

$(document).ready(function(){      $('a.test').bind("click", showdiv());  }); 

thanks

you want pass reference function callback, , not result of function execution:

showdiv() returns value; if no return statement used, undefined returned.

showdiv reference function should executed.

this should work:

$(document).ready(function(){     $('a.test').bind("click", showdiv); }); 

alternatively, use anonymous function perform more advanced function:

...bind('click', function(){   foo.showdiv(a,b,c);   ...more code... }); 

in circumstances may want use value returned function callback:

function function foo(which) {   function bar()   {     console.log('so true');   }   function baz()   {     console.log('no way!');   }   return ? bar : baz; }  ...click( foo( fizz ) ); 

in example, foo evaluated using fizz , returns function assigned callback click event.