jQuery: $().click(fn) vs. $().bind('click',fn);
Solution 1:
For what it's worth, from the jQuery source:
jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," +
"mousedown,mouseup,mousemove,mouseover,mouseout,mouseenter,mouseleave," +
"change,select,submit,keydown,keypress,keyup,error").split(","), function(i, name){
// Handle event binding
jQuery.fn[name] = function(fn){
return fn ? this.bind(name, fn) : this.trigger(name);
};
});
So no, there's no difference -
$().click(fn)
calls
$().bind('click',fn)
Solution 2:
+1 for Matthew's answer, but I thought I should mention that you can also bind more than one event handler in one go using bind
$('#myDiv').bind('mouseover focus', function() {
$(this).addClass('focus')
});
which is the much cleaner equivalent to:
var myFunc = function() {
$(this).addClass('focus');
};
$('#myDiv')
.mouseover(myFunc)
.focus(myFunc)
;
Solution 3:
There is one difference in that you can bind custom events using the second form that you have. Otherwise, they seem to be synonymous. See: jQuery Event Docs