From http://api.jquery.com/on/:
.on( events [, selector ] [, data ], handler(eventObject) )
I know this might sound a little stupid, but could anyone explain the syntax here?
What does the [] mean? I would think it means that you could add several options (selectors/data) but as you can also add several events, why does events
not have square brackets?
Also here's an example .on():
$(document).on("click", ".item", function() {
alert("hi");
});
Where does the data
written in the method syntax come into play here?
The square brackets indicate that an argument is optional. For the .on()
method, both selector
and data
are optional, but events
and handler
are required.
For example:
$(something).on("click", function () {});
// ^ events ^ handler
$(something).on("click", ".child", function () {});
// ^ events ^ selector ^ handler
$(something).on(function () {}); // Won't work, missing events argument
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With