tags:

views:

93

answers:

2

I have a function:

function foo() { console.log('i run!'); }

And a binding to a button:

$(function() { 
   $('#myButton').click(foo);
});

I am almost positive that the console should not show 'i run!' until the button is clicked. I have no other #myButtons on the page (I have forple checked this), and yet foo continues to run when another unrelated button is clicked. There are no other references to foo (again, forple checked).

What could be happening here? Am I binding wrong?

+3  A: 

You're not closing you're document ready block properly, it is missing a closing parenthesis (and optionally a semicolon), try:

$(function() { 
   $('#myButton').click(foo);
});
karim79
ehh... good catch, but i have it correct in my code... sorry... edited
Jason
Is there anything amiss in #myButton's markup (for instance, a missing '>'). Broken markup can cause weird behaviour. Maybe paste your #myButton code into the question. I honestly can't think of anything else!
karim79
A: 

Shouldn't the function look like this?

$(function() { 
   $('#myButton').click(function(){ foo() });
});
fudgey
That's only necessary if you want foo() to retain the element reference in the value of 'this'.
Matt Huggins
Thanks for the clarification, it's not really obvious from the jQuery documentation.
fudgey