tags:

views:

278

answers:

6

Is there any benefit to using .click() over .bind('click') or vice-versa besides the amount of typing you have to do?

+3  A: 

The .click() is shorthand of .bind('click'), you can use either of them.

Sarfraz
correct me if I'm wrong, but that means its an extra function call, and therefore slower in situations where that matters.
Erik
@Erik: Yup, you are right, it is an extra call to the bind function. It is up to the users whether they want brevity or performance.
Sarfraz
Well, since the question is "is there any benefit to ... " shouldn't you answer be "Yes, speed."
Erik
@Erik: Good point, I should have written that, OP should see this comment, thanks :)
Sarfraz
@Erik that was my other thought, if speed is an issue on my projects i'll use `.bind()`
Sam
@Sam - You're kidding me. What kind of speed issue could you ever run into that an extra function call would kill you. If extra method calls ever become an issue, then I would suggest you not use jQuery at all. LOL!
Gutzofter
@Gutzofter - I tend to agree with @Erik's point (see the chosen answer) that any speed increase can only be a positive thing!
Sam
+2  A: 

Who knew google have the answer!

http://stackoverflow.com/questions/518762/jquery-clickfn-vs-bindclick-fn

GaVrA
A: 

you can unbind a click event when you use the bind function and not when you use the click. Which is sometimes handy.

Ayrton
Can anyone else verify this? Seems like a major advantage if it's correct.
Sam
If `$.click` is short hand for a `$.bind('click, //` then a `$.unbind` will work on a `$.click`
Gutzofter
This is 100% incorrect. [Here's Proof, and yes its a jsbin link](http://jsbin.com/azaba3/edit)
Erik
well it's def. something I didn't know, thanks for pointing it out
Ayrton
+1  A: 

click() is basically a shortcut to bind('click').

Unlike click() you can pass data with a 2nd (optional) parameter of bind() to the event handler that is specified as a 3rd parameter:

.bind( eventType, [ eventData ], handler(eventObject) )

See http://api.jquery.com/bind/ for more.

Ain
+1  A: 

I would start to use jQuery live handling

$('a.ajax').live('click',processAjaxItem());

if your using bind and .click the items that are in the dom at the time will only be binded with your function, so whatever you content you pull in via ajax that match your expression will not be "bound"

Where as if you use live then links containing ajax class that come from ajax content after you have run the function will also be binded.

RobertPitt
There are disadvantages to `live()` too. Once you start adding a lot of events, especially if bound to complicated selectors, the per-event checking mechanism can start to get very slow. Also, whilst `click` is OK, some other events don't quite work properly with `live()` on all browsers; jQuery tries to work around some of these cases, but with varying degrees of success. Probably best not resorting to `live()`/`delegate()` binding until you actually need to.
bobince
Very interesting point that i never knew myself, i havent studied the mechanism of live() but i will look into that for my own personal knowledge. Thankyou
RobertPitt
+4  A: 

Based on some very unscientific and very quick tests using .bind vs .click represents a 15% (roughly) speed up, as tested over 50,000 iterations of each.

Its not huge, unless you're binding massive numbers of events, but I'm always of the thought that making it faster when it takes no effort is something worth doing.

My quick & dirty test: http://jsbin.com/ixegu/edit

Other Benefits - Bind Multiple Events

Since you accepted this answer, I thought I'd add a bit more. You can also bind multiple events with .bind, for example:

$('#link').bind('mouseover focus', function() { ... });

There's another syntax for binding multiple events via the bind() method. From the docs:

$("div.test").bind({
  click: function(){
    $(this).addClass("active");
  },
  mouseenter: function(){
    $(this).addClass("inside");
  },
  mouseleave: function(){
    $(this).removeClass("inside");
  }
});

Pass extra data on event with bind()

Then there's the ability to pass arbitrary data when you create the bind (only at the time you create the bind)

<div id="link">Happy</div>
<div id="otherlink">Sad</div>

function myHandlerFunc(e) {
  alert('This is a ' + e.data.myVal + ' function.');
}

$('#link').bind('click', { myVal : 'Happy' } , myHandlerFunc);
$('#otherlink').bind('click', { myVal: 'Sad' }, myHandlerFunc);

The above alerts "This is a happy link" when you click on happy, and "This is a sad link" when you click on sad. See http://jsbin.com/idare/edit for an example.

Read the docs

You can always find out more here.

Erik
+1 for some actual profiling.
Gutzofter