I have a huge jQuery application, and I\'m using the below two methods for click events.
First method
Neither one is better in that they may be used for different purposes. onClick
(should actually be onclick
) performs very slightly better, but I highly doubt you will notice a difference there.
It is worth noting that they do different things: .click
can be bound to any jQuery collection whereas onclick
has to be used inline on the elements you want it to be bound to. You can also bind only one event to using onclick
, whereas .click
lets you continue to bind events.
In my opinion, I would be consistent about it and just use .click
everywhere and keep all of my JavaScript code together and separated from the HTML.
Don't use onclick
. There isn't any reason to use it unless you know what you're doing, and you probably don't.