views:

65

answers:

4

For example, if I have:

function example() {
    alert("example");
}

Then why doesn't this work?

$("h2").click( example() );

Does it have to be defined inline?

+7  A: 

Nope, but the parens is making the function execute. Use this:

$("h2").click( example );
moff
+2  A: 

you want to just do $("h2").click(example); - example should have no parenthesis

Marek Karbarz
+2  A: 
function example() {
    alert("example");
}

$("h2").click(example);
Chris Tek
+1  A: 

just use

$("h2").click(example);

or

$("h2").click(function() { example(); });
ithcy