1

What is the difference Between the following statements. Which one should I choose over one another in a situation.

$(document).on("click","#btn",callback);

$("#btn").on("click",callback);
3
  • 3
    The first line is an example of event delegation. It's in the docs. You do it when there's a possibility that #btn doesn't exist yet when the code is run. Commented Dec 5, 2013 at 1:37
  • 1
    This is Event Delegation vs Direct Binding, this SO answer is pretty good stackoverflow.com/questions/8110934/… you can search "jQuery Event delegation vs direct" and get a lot of helpful results...one main difference is that with delegation you can account for new, dynamically created elements
    – A.O.
    Commented Dec 5, 2013 at 1:39
  • Search documentation first - especially since the method name is known. Commented Dec 5, 2013 at 1:41

3 Answers 3

2

Have a look at the event delegation jQuery documentation

basically

$(document).on("click","#btn",callback);

will bind the click to the document DOM rather than the element directly which is useful when you are appending DOM elements to the window in which case the #btn selector will not exist yet.

0

Basically wiht direct

$(document).on("click","#btn",callback);

you're assigning events to document that affect matching descendants (#btn) which may or may not exist at the time that line of code is run.

$("#btn").on("click",callback);

With delegated you're affecting all matching elements that do exist at the time that line of code is run, and no matching ones that are created later.

http://api.jquery.com/on/

Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on(). To ensure the elements are present and can be selected, perform event binding inside a document ready handler for elements that are in the HTML markup on the page. If new HTML is being injected into the page, select the elements and attach event handlers after the new HTML is placed into the page. Or, use delegated events to attach an event handler, as described next.

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.

0

direct

$("#btn").on("click",callback);

Element with id present in DOM get clicked call callback. If it is present in DOM handler will get attached.

binds the event listener directly to the elements


delegated

Read Event Delegation.

Use .on()

$(document).on("click","#btn",callback);

When a element in document is added with id btn than on click of that element run callback function .

Binds the event listener to the document object

Syntax

$( elements ).on( events, selector, data, handler );

Not the answer you're looking for? Browse other questions tagged or ask your own question.