I have a huge jQuery application, and I'm using the below two methods for click events.
First method
HTML
<div id="myDiv">Some Content</div>
jQuery
$('#myDiv').click(function(){
//Some code
});
Second method
HTML
<div id="myDiv" onClick="divFunction()">Some Content</div>
JavaScript function call
function divFunction(){
//Some code
}
I use either the first or second method in my application. Which one is better? Better for performance? And standard?
Difference in works. If you use click(), you can add several functions, but if you use an attribute, only one function will be executed - the last one.
DEMO
HTML
JavaScript
If we are talking about performance, in any case directly using is always faster, but using of an attribute, you will be able to assign only one function.
Neither one is better in that they may be used for different purposes.
onClick
(should actually beonclick
) 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 whereasonclick
has to be used inline on the elements you want it to be bound to. You can also bind only one event to usingonclick
, 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.onclick, onmouseover, onmouseout, etc. events are actually bad for performance (in Internet Explorer mainly, go figure). If you code using Visual Studio, when you run a page with these, every single one of these will create a separate SCRIPT block taking up memory, and thus slowing down performance.
Not to mention you should have a separation of concerns: JavaScript and layouts should be separated!
It is always better to create evenHandlers for any of these events, one event can capture hundreds/thousands of items, instead of creating thousands of separate script blocks for each one!
(Also, everything everyone else is saying.)
Using
$('#myDiv').click(function(){
is better as it follows standard event registration model. (jQuery internally usesaddEventListener
andattachEvent
).Basically registering an event in modern way is the unobtrusive way of handling events. Also to register more than one event listener for the target you can call
addEventListener()
for the same target.http://jsfiddle.net/aj55x/1/
More about Modern event registration -> http://www.quirksmode.org/js/events_advanced.html
Other methods such as setting the HTML attributes, example:
Or DOM element properties, example:
are old and they can be over written easily.
HTML attribute should be avoided as It makes the markup bigger and less readable. Concerns of content/structure and behavior are not well-separated, making a bug harder to find.
The problem with the DOM element properties method is that only one event handler can be bound to an element per event.
More about Traditional event handling -> http://www.quirksmode.org/js/events_tradmod.html
MDN Reference: https://developer.mozilla.org/en-US/docs/DOM/event
From what I understand, your question is not really about whether to use jQuery or not. It's rather: Is it better to bind events inline in HTML or through event listeners?
Inline binding is deprecated. Moreover this way you can only bind one function to a certain event.
Therefore I recommend using event listeners. This way, you'll be able to bind many functions to a single event and to unbind them later if needed. Consider this pure JavaScript code:
This works in most modern browsers.
However, if you already include jQuery in your project — just use jQuery:
.on
or.click
function.$('#myDiv').click
is better, because it separates JavaScript code from HTML. One must try to keep the page behaviour and structure different. This helps a lot.