Reputation: 19651
What is difference between $(".anything").click() and $(".anything").bind(click)
$(".anything").click(function() {
});
$(".anything").bind('click', function() {
});
Upvotes: 3
Views: 320
Reputation: 681
See this post, which points to the JQuery source to show that .click(fn) just calls .bind('click', fn): jQuery: $().click(fn) vs. $().bind('click',fn);
I usually only use the latter if:
var fn = function() { alert('foo'); } $('#foo').bind('click', fn); ... $('#foo').unbind('click', fn);
Upvotes: 2
Reputation: 240829
In that specific case, absolutely nothing.
However:
A) If you give .click()
no argument, it triggers the event instead of setting the handler.
B) only .bind()
lets you use the "namespaced" way of registering handlers:
$(whatever).bind('click.myEvent', function (e) { ... });
$(whatever).unbind('click.myEvent'); // Removes just that handler
Upvotes: 4
Reputation: 1109865
The first is a shortcut of the second. The second is actually wrong, click
should have been quoted. Further, in the second you have the added benefit that you can bind the same function to multiple events, each separated by a space. E.g.
$(".anything").bind("click keypress blur", function() {
});
Upvotes: 6