Andrew
Andrew

Reputation: 10043

In JQuery when should you use .bind() over .click() - or any other given event?

I understand the difference between Live and Bind but when should I use use .bind() over a 'standard' event method as shown below.

Are there any key differences in the way these two calls work?

$('.clickme').bind('click', function() {
  // Handler called.
});

$('.clickme').click(function() {
  // Handler called.
});

Upvotes: 11

Views: 3893

Answers (5)

Raynos
Raynos

Reputation: 169551

use .bind when you want to bind to "event.namespace"

Actaully almost always use .bind and almost always use namespaces.

Upvotes: 0

Shawn Chin
Shawn Chin

Reputation: 87004

They're effectively the same. However, using bind() allows you to make use of namespaced events. This is especially useful when writing plugins.

Upvotes: 11

Ergec
Ergec

Reputation: 11834

in "bind" you can use multiple events

$('#foo').bind('mouseenter mouseleave', function() {
  $(this).toggleClass('entered');
});

Upvotes: 4

cmar
cmar

Reputation: 161

I use the explicit methods when available. I use the bind when a method isn't available like for window.onbeforeunload

The other time to use bind is if your are developing and switching between "live" and "bind".

Upvotes: 0

bpruitt-goddard
bpruitt-goddard

Reputation: 3324

They are the same. See here

Upvotes: 2

Related Questions