0

I have been searching around without finding an satisfying answer. What is the difference between the 2 following event-handlers.

$('#div').on('click', function(){
    //do something
});

and

$('#div').click(function(){
    //do something
});

I know that in the $('#div').on('click', function(), you can send in more div or class-selectors in addition to 'click', but is that it?

I'd love to hear what else separates them, and if there are times where one is better to use than the other.

Thanks

8
  • 4
    click() is the shorthand for on('click'). That's it (in your use-case).
    – dsgriffin
    Commented Jul 7, 2013 at 15:02
  • 2
    There are no differences. Here is another post identical: stackoverflow.com/questions/9122078/… Commented Jul 7, 2013 at 15:04
  • @Zenith Btw, yesterday you answered one of my other post - turned out you were right- the fiddle you provided me did work :) but you deleted your answer or something? Anyway - thanks!
    – Nilzone-
    Commented Jul 7, 2013 at 15:09
  • 2
    @PhilippM No. .bind have been deprecated in favor for .on.
    – mekwall
    Commented Jul 7, 2013 at 15:11
  • 2
    @PhilippM: The click documentation was updated to "This method is a shortcut for .on('click', handler)" Commented Jul 7, 2013 at 15:12

1 Answer 1

0

.on() can be used to bind event to elements that is not present at the page load, but may appear later. There are other benefits also.

Not the answer you're looking for? Browse other questions tagged or ask your own question.