2

I'm aware both these examples do the same thing, but are their any hidden differences? Is one faster than the other? Is it just a preferance?

$('#div1').on('click', function() {
$('#div2').toggle();
});



$('#div1').click(function() {
$('#div2').toggle();
});
Mark Shakespeare
  • 115
  • 1
  • 3
  • 9

2 Answers2

2

No, there is no difference in performance. Second one is just short hand for writing first one.

Panther
  • 3,312
  • 9
  • 27
  • 50
0

As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document.:http://api.jquery.com/bind/, http://api.jquery.com/on/

Also the the answer from this stackoverflow: Difference between .on('click') vs .click()

Community
  • 1
  • 1
Travis Pettry
  • 1,220
  • 1
  • 14
  • 35