2

Possible Duplicate:
jquery on vs click methods

I understand that $(parent).on('click', 'element', function(){ ... is used to attach a click handler to an element that is dynamically created - while $('element').click(... only works if the element is present at inital page load. As of lately I always use .on() for everything because it covers both - my question is - is there any time where .click is preferred , or any other advantages that I may not know of to not using .on()

Community
  • 1
  • 1
Scott Selby
  • 9,420
  • 12
  • 57
  • 96
  • possible duplicate of [jquery on vs click methods](http://stackoverflow.com/questions/8601482/jquery-on-vs-click-methods) and http://stackoverflow.com/questions/9122078/difference-between-onclick-vs-click – j08691 Sep 20 '12 at 02:51
  • It's just a matter of preference really, some say that `click` has a bit worse performance but not relevant for _real_ stuff I'd say. – elclanrs Sep 20 '12 at 02:57

2 Answers2

9

Actually .click() is handled by .on(), see: https://github.com/jquery/jquery/blob/master/src/event.js#L1014

So the only reason to prefer .click() - is to have shorter code

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • that's exactly what I was looking for , I just wasn't sure , someone had put that as an answer to the other question , but no upvotes and no one accepted it so I wasn't sure – Scott Selby Sep 20 '12 at 03:01
  • Also , that was the point of the question , because someone else will always respond with the opposite , thanks for the link – Scott Selby Sep 20 '12 at 03:02
-1

As per http://api.jquery.com/click/, .Click is a shorcut for .bind(),.on() and .trigger() in various scenarios.

For dynamically created elements, .live() is the exact method of attaching handlers to them.

To answer your question, .Click() is widely used for its short form of use, as we use $(function(){}) instead of $(document).ready().

Dhanasekar Murugesan
  • 3,141
  • 1
  • 18
  • 21