5

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

Sam
  • 4,437
  • 11
  • 40
  • 59
  • 1
    possible duplicate of [jQuery: $().click(fn) vs. $().bind('click',fn);](http://stackoverflow.com/questions/518762/jquery-clickfn-vs-bindclick-fn) – chakrit May 30 '10 at 14:27

6 Answers6

10

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
  • 20,526
  • 8
  • 45
  • 76
3

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

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • 2
    correct me if I'm wrong, but that means its an extra function call, and therefore slower in situations where that matters. – Erik May 30 '10 at 14:31
  • @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 May 30 '10 at 14:34
  • Well, since the question is "is there any benefit to ... " shouldn't you answer be "Yes, speed." – Erik May 30 '10 at 14:40
  • @Erik: Good point, I should have written that, OP should see this comment, thanks :) – Sarfraz May 30 '10 at 14:44
  • @Erik that was my other thought, if speed is an issue on my projects i'll use `.bind()` – Sam May 30 '10 at 15:37
  • 1
    @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 May 30 '10 at 15:59
  • @Gutzofter - I tend to agree with @Erik's point (see the chosen answer) that any speed increase can only be a positive thing! – Sam May 31 '10 at 08:20
3

Who knew google have the answer!

jQuery: $().click(fn) vs. $().bind('click',fn);

Community
  • 1
  • 1
Gavrisimo
  • 1,827
  • 5
  • 25
  • 33
1

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 Tohvri
  • 2,987
  • 6
  • 32
  • 51
1

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
  • 56,863
  • 21
  • 114
  • 161
  • 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 May 30 '10 at 14:58
  • 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 May 30 '10 at 16:39
0

You may also notice the benefit of "bind" if you have the same handler for two or more events. In fact, The use of "bind" avoids redundancy. example:

$('#myId').bind('click mouseenter mouseleave',
      function(){
        $(this).toggleClass('myClass');
     }
);

Even if the handler function is almost the same except for a few instructions, using (BIND) is still useful but must be used (event.type) to differentiate between events.

$('#myId').bind('click mouseenter mouseleave',
              function(e){
                $(this).toggleClass('myClass');
                 if(e.type ==='click'){
                        //do sthg
                  }
             }
    );
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254