Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any drawbacks to listen events on document level in jQuery?

To listen events on elements, I think to listen on document level:

$(document).on('click', '.myclass', function() {/*do something*/});

is better than the style to listen on element level:

$('.myclass').on('click', function() { /*do something*/ }); 

The reason is the first style could apply to dynamically added new elements as well. You can also see this style used a lot in Bootstrap: https://github.com/twitter/bootstrap/blob/master/js/bootstrap-alert.js

I would like to use the first style extensively. But I wonder if there are any drawbacks of this style, say performance?

like image 600
Billy Chan Avatar asked May 05 '13 10:05

Billy Chan


People also ask

What method can be used to listen for DOM events?

The addEventListener() method allows you to add event listeners on any HTML DOM object such as HTML elements, the HTML document, the window object, or other objects that support events, like the xmlHttpRequest object.

Do event listeners get removed?

According to the jquery Documentation when using remove() method over an element, all event listeners are removed from memory. This affects the element it selft and all child nodes. If you want to keep the event listners in memory you should use . detach() instead.

Why do we use jQuery to wire up events?

jQuery makes it straightforward to set up event-driven responses on page elements. These events are often triggered by the end user's interaction with the page, such as when text is entered into a form element or the mouse pointer is moved.


2 Answers

From the jQuery documentation available here:

Event performance

In most cases, an event such as click occurs infrequently and performance is not a significant concern. However, high frequency events such as mousemove or scroll can fire dozens of times per second, and in those cases it becomes more important to use events judiciously. Performance can be increased by reducing the amount of work done in the handler itself, caching information needed by the handler rather than recalculating it, or by rate-limiting the number of actual page updates using setTimeout.

Attaching many delegated event handlers near the top of the document tree can degrade performance. Each time the event occurs, jQuery must compare all selectors of all attached events of that type to every element in the path from the event target up to the top of the document. For best performance, attach delegated events at a document location as close as possible to the target elements. Avoid excessive use of document or document.body for delegated events on large documents.

jQuery can process simple selectors of the form tag#id.class very quickly when they are used to filter delegated events. So, "#myForm", "a.external", and "button" are all fast selectors. Delegated events that use more complex selectors, particularly hierarchical ones, can be several times slower--although they are still fast enough for most applications. Hierarchical selectors can often be avoided simply by attaching the handler to a more appropriate point in the document. For example, instead of $("body").on("click", "#commentForm .addNew", addComment) use $("#commentForm").on("click", ".addNew", addComment).

like image 147
Phylogenesis Avatar answered Oct 26 '22 19:10

Phylogenesis


If .myclass is added dynamically in your document, this is what you prefer

$(document).on('click', '.myclass', function() {/*do something*/});

To make it more efficient, consider this - (As you would like to use the first style extensively)

<div class='parentClass'>
   <div class='myclass'></div>  // <-- added dynamically
</div>

You can do this -

$('.parentClass').on('click', '.myclass', function() {/*do something*/});

See Api Docs - http://api.jquery.com/on/

like image 44
Mohammad Adil Avatar answered Oct 26 '22 18:10

Mohammad Adil