11

What is the difference between the following lines of code, or are they just 2 different ways to write the same thing:

$("p").click(function() { some code here });

$("p").bind("click", function(){ some other code here });

Am I over simplifying this? Because if you wanted to bind more than one event you could just chain the events, correct?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Mike Fielden
  • 10,055
  • 14
  • 59
  • 99

3 Answers3

14

It also allows you to bind the same anonymous method to multiple events like:

$("p").bind("click dblclick mouseover mouseout", function(){ some other code here });
John Boker
  • 82,559
  • 17
  • 97
  • 130
6

Also note that binds allows for custom events

$(elem).bind('myEvent', function(){
   alert('myEvent!');
});
$(elem).trigger('myEvent'); //alerts 'myEvent!'
Pim Jager
  • 31,965
  • 17
  • 72
  • 98
4

The first version is just a shorthand for the second one.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194