Well, what is the difference between the two methods? Is it semantically the same or does it affect something?
$('#demo').on('click', function() {
alert('Click con on.click');
});
$('#demo2').click(function() {
alert('Click con click');
});
#demo{
background-color:#F2F2F2;
width:500px;
height:100px;
text-align:center;
vertical-align: middle;
display:table-cell;
cursor:pointer;
}
#demo2{
background-color:#E6E6E6;
width:500px;
height:100px;
text-align:center;
vertical-align: middle;
display:table-cell;
cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="demo">Click con on.click</div>
<div id="demo2">Click con click</div>
Perhaps the most important difference is that it
.on('click', selector, function(){})
allows you to work with elements added after the DOM has been loaded, whereas.click(function(){})
this does not.Other differences:
.on()
creates a single handler for all the elements included in the selector, while.click()
creating a handler for each of these elements.on()
allows the use of namespaces.I would like to add something regarding this topic. The method
.on
allows delegation of events, but can also be usedlistener
directly by varying the syntax.The point is to provide it with a selector in the call. So:
$(document).on('click', myEventHandler)
you will hear all the clicks of the entire document.$('#myElement').on('click', myEventHandler)
it will listen for all clicks from all descendants of myElement (and itself).$('#myElement').on('click', '.mySelector', myEventHandler)
what it does is the same as above but then it filters by.mySelector
before callingmyEventHandler
.So:
$('#myElement').on('click', myEventHandler)
it is exactly the same asThe reason that
on
"allows to work with elements added after the DOM has been loaded" is that there is actually alistener
for that event declared in a lower level element that is encompassing it.More clear:
Actually the one that is receiving the event is the one
listener
declared for , then it filters all the elements it containsmyDIV
by selector .button
That's why adding a new button element to
myDIV
it reacts to the eventclick
.Easy! ;)
An everyday example. When we use a component of some
framework
such as aalert
ofBootstrap
. We see that by dynamically adding the alert to the DOM with thedata-dismiss="alert"
automatically attribute, the close button causes the alert to close without the need to attach alistener
. That's because internally the Bootstrap js library what it does is:Then
myHandler
it does the of the closestdismiss
class element (method ), etc.alert
jQuery.closest
How do you
jQuery
identify which alert button was clicked on? Thanks to theevent bubbling
... Even though thelistener
was set on the root element (document
), the event actuallyclick
occurred on the alert button and worked its way down until it received itdocument
. Then itjQuery
filtered all thealerts
and calledhandler
through the methodapply
andjavascript
passing as a parameter the element that originated the event (thebutton
).When we use
$(document).on('click', 'button.myClass', myHandler)
jQuery
what it does internally it isdocument.addEventListener('click', myHandler, false)
.So it captures all
clicks
of all the objects on the page and then does thedispatch
to objectbutton.myClass
.