jQuery.on callback parameters

3k Views Asked by At

In the documentation for .on(), the parameters are given as follows:

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

With regards to the handler parameter (i.e. the callback function):

handler

Type: Function( Event eventObject [, Anything extraParameter ] [, ... ] )

A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false.

When are any extraParameter arguments passed to the callback function? There are a lot of aliases for .on() but I haven't come across any that pass more than the eventObject parameter.

2

There are 2 best solutions below

3
On BEST ANSWER

There is an example in the documentation:

$( "div" ).on( "click", function( event, person ) {
  alert( "Hello, " + person.name );
});

//You can trigger an event without user action
$( "div" ).trigger( "click", { name: "Jim" } );
0
On

When are any extraParameter arguments passed to the callback function?

Utilizing .trigger(eventType [,extraParameters])

var obj = $({})

obj.on("evt", function(e, a, b) {
  console.log(e, a * b)
});


obj.trigger("evt", [Math.random(), 100])
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>