$('a#foo').click(Foo()); $('a#foo').bind(Foo());
Foo provides you this function, but adds () after it means that you are calling the function instead of passing the function itself. Since you are calling the function, false ends up being passed in click and bind obviously does nothing. Some of your other problems may arise due to the fact that you simulate switching to this tab twice (calling the event handler twice).
var Foo = function() { //same content and calls as before return false; } $('a#foo').click(Foo); $('a#foo').bind(Foo);
^^ should do what you want.
Alternatively, is there a better way to achieve the results I'm looking for?
Right now, all we really know about your design is what you call with the click event handler to switch tabs. This part is awesome, but we need more information to give you the deeper answer you really want. If you put the code inside Foo , we can help a little more .: D
EDIT: credit for SLaks โฆ to notify new in a function declaration that I missed. I will add a few details to his explanation:
When you write var foo = new function (...) {...}, you create functions, then calling its constructor.
It is equivalent
var Someclass = function (...) {...}; var foo = new SomeClass;
without the dummy variable SomeClass.
function() {} is an anonymous function, as you would expect. new in javascript is a bit confusing. When you call a function and precede it with new , you use this function to instantiate the class defined in the function. In JS, unlike most other languages, the entire class definition is in a single constructor function, from which you specify all instance variables, for example:
Foo = function() { this.a = "lala"; this.b = 5; }
To create class instance methods, you use the prototype attribute. However, I just realized that I have a super theme. More about this here and here .: D