42

I playing around with a function that I want to bind to all the links. At the present the function fires when the page loads, instead of when I click on the link.

Here's my code. (I can post the function showDiv(), if you need to see it.) Can you tell if I'm doing something wrong or stupid here?

$(document).ready(function(){

    $('a.test').bind("click", showDiv());

});

Thanks!

Jeff
  • 3,723
  • 8
  • 38
  • 64
  • 1
    A few notes on how to write code the "jQuery way", use `jQuery(function($){ $('a.test').click(showDiv); });`. If jQuery's factory function receives a function it automatically uses it as a `document.ready` callback. The `document.ready` callback will be sent the jQuery object as the first parameter, which allows you to alias `jQuery` to `$` to prevent any possible multi-library issues. Also, use the event shortcuts for improved readability. – zzzzBov Aug 18 '11 at 04:52
  • By the way: in newer versions `.on` should be used https://stackoverflow.com/questions/11847021/jquery-s-bind-vs-on – marsze Nov 14 '18 at 11:22

4 Answers4

73

You want to pass a reference to a function as a callback, and not the result of function execution:

showDiv() returns some value; if no return statement was used, undefined is returned.

showDiv is a reference to the function that should be executed.

This should work:

$(document).ready(function() {
  $('a.test').on("click", showDiv); // jQuery 1.7 and higher
  $('a.test').bind("click", showDiv); // jQuery 1.6 and lower
});

Alternatively, you could use an anonymous function to perform a more advanced function:

// jQuery 1.7 and higher
el.on('click', function() {
  foo.showDiv(a, b, c);
  // more code...
});

// jQuery 1.6 and lower
el.bind('click', function() {
  foo.showDiv(a, b, c);
  // more code...
});

In some circumstances you may want to use the value returned by a function as a callback:

function function foo(which) {
  function bar() {
    console.log('so very true');
  }

  function baz() {
    console.log('no way!');
  }

  return which ? bar : baz;
}

el.click(foo(fizz));

In this example, foo is evaluated using fizz and returns a function that will be assigned as the callback for the click event.

Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303
zzzzBov
  • 157,699
  • 47
  • 307
  • 349
  • 2
    In particular, `showDiv()` *invokes* the function-object evaluated from the *expression* `showDiv` -- `showDiv` itself merely evaluates to a function-object (in this instance) which can be used as a callback. (There are no "references" ;-) –  Aug 18 '11 at 04:40
  • 2
    +1 This was happening to me and for the life of me I couldn't figure it out, after reading your answer it was obvious.. D'oh! – MattSizzle Jun 08 '14 at 01:11
  • 1
    I tried every which way, and using the function name alone did not work for me. Finally the anon function worked. Thanks. – punstress May 13 '15 at 09:39
  • 1
    @MrGuliarte, read the part that starts "Alternatively, you could use an anonymous function to perform a more advanced function" – zzzzBov Sep 15 '15 at 02:33
  • I know this is super old post, but I'm in the having the same problem as OP. But none of the solution here work. I'm trying to attached onclick to Body element using anonymous function inside "$(document).ready". But the anonymous function just fired up right after page load : $('body').on('click', function() { alert("Clicked On MyClass element"); }); – KMC Dec 20 '16 at 20:15
  • @user30646, create a [mcve] and invest some time debugging the actual issue. The code you've provided works just fine. – zzzzBov Dec 20 '16 at 20:32
6

Looks like you're calling the function showDiv directly there (and binding the return result of showDiv() to the click handler instead of binding it directly.

You want something like

$(document).ready(function() { $('a.test').bind("click", showDiv); });
tjarratt
  • 1,636
  • 9
  • 17
5

Use the below line. showDiv() will call the function rigth away when that line is executed.

$('a.test').bind("click", showDiv);
ShankarSangoli
  • 67,648
  • 11
  • 84
  • 121
4

Change it to: $('a.test').bind("click", showDiv); (do not put parens around showDiv since you want to pass the function reference).

Mrchief
  • 70,643
  • 19
  • 134
  • 181