Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding jQuery API Documentation Syntax

Tags:

jquery

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?

like image 219
frrlod Avatar asked Oct 21 '22 11:10

frrlod


1 Answers

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
like image 135
James Allardice Avatar answered Nov 04 '22 20:11

James Allardice