Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: difference between .click() AND .on("click")

I usually use

$(selector).click(... 

But some people recommend me to use this instead:

$(selector).on("click", function(... 

Or $(selector).live("click"... (Deprecated)

I read the manual, but my begginer's mind couldn't understand it. I got confused with all the terminology they used. I still do not know the difference nor why to use .on()
http://api.jquery.com/on/

like image 706
Omar Avatar asked Nov 05 '11 07:11

Omar


People also ask

What is the difference between .on click function ()) and .click function?

So onclick creates an attribute within the binded HTML tag, using a string which is linked to a function. Whereas . click binds the function itself to the property element.

What is difference between Onclick and click?

click is a function on HTML elements you can call to trigger their click handlers: element. click(); onclick is a property that reflects the onclick attribute and allows you to attach a "DOM0" handler to the element for when clicks occur: element.

What is Click () method?

click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.

What is click in jQuery?

jQuery click() MethodThe click event occurs when an element is clicked. The click() method triggers the click event, or attaches a function to run when a click event occurs.


1 Answers

At the end of the day, every event is bound to some element in the DOM. In the case of .bind, you're binding directly to the element (or elements) in your jQuery object. If, for example, your jQuery object contained 100 elements, you'd be binding 100 event listeners.

In the case of .live, .delegate, and .on, a single event listener is bound, generally on one of the topmost nodes in the DOM tree: document, document.documentElement (the <html> element), or document.body. Because DOM events bubble up through the tree, an event handler attached to the body element can actually receive click events originating from any element on the page. So, rather than binding 100 events you could bind just one.

For a small number of elements (fewer than five, say), binding the event handlers directly is likely to be faster (although performance is unlikely to be an issue). For a larger number of elements, always use .on.

The other advantage of .on is that if you add elements to the DOM you don't need to worry about binding event handlers to these new elements. Take, for example, an HTML list:

<ul id="todo">   <li>buy milk</li>   <li>arrange haircut</li>   <li>pay credit card bill</li> </ul> 

Next, some jQuery:

// Remove the todo item when clicked. $('#todo').children().click(function () {   $(this).remove() }) 

Now, what if we add a todo?

$('#todo').append('<li>answer all the questions on SO</li>') 

Clicking this todo item will not remove it from the list, since it doesn't have any event handlers bound. If instead we'd used .on, the new item would work without any extra effort on our part. Here's how the .on version would look:

$('#todo').on('click', 'li', function (event) {   $(event.target).remove() }) 
like image 72
davidchambers Avatar answered Sep 21 '22 01:09

davidchambers