Understanding JQuery API Documentation Syntax

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

.on( events [, selector ] [, data ], handler(eventObject) )

I know this may seem a little silly, but can anyone explain the syntax here?

What does [] mean? I would think that this means you can add multiple options (selectors / data), but since you can also add multiple events, why eventsdon't they have square brackets?

Also here is an example of .on ():

    $(document).on("click", ".item", function() {
alert("hi");
});

Where is used here data, recorded in the method syntax?

+5
source share
3 answers

Square brackets indicate that the argument is optional. For the method of .on()the two selector, and dataare optional, but required events, and handler.

:

$(something).on("click", function () {});
//                 ^ events    ^ handler

$(something).on("click", ".child", function () {});
//                ^ events   ^ selector   ^ handler

$(something).on(function () {}); // Won't work, missing events argument
+2

[] , . selector data.

data.

+1

Square brackets indicate that the parameter is optional. That way, you can optionally provide a selector (for delegating events) or data (for use inside an event handler function), but you don't need to.

For your specific example, no value is passed data(because you don't need it). In the code for the jQuery function, onit determines which parameter is actually used for the type-based value.

0
source

All Articles