2

I have built a jQuery custom function

jQuery.fn.myfunction = function(){ ... }

This custom function is called using .trigger()

my_element.trigger('change', [ "Custom", "Event" ]);

I am trying to retrieve the array [ "Custom", "Event" ] in the custom function myfunction but I can't find how to do so.

Any help? many thanks

Vincent
  • 1,651
  • 9
  • 28
  • 54

1 Answers1

2

The first parameter is the event object. All following parameters are custom.

jQuery.fn.myfunction = function(e,customParm1,customParm2){ ... }

See the .trigger() documentation:

$( "#foo" ).on( "custom", function( event, param1, param2 ) {
  alert( param1 + "\n" + param2 );
});
$( "#foo").trigger( "custom", [ "Custom", "Event" ] );

If you really need an array of all custom args, then you can just use:

var customArgs = Array.prototype.slice.call(arguments); // convert arguments to array
customArgs.shift(); // get rid of event arg
console.log( customArgs ); // outputs [ "Custom", "Event" ]

http://jsfiddle.net/rq3hj03x/

JDB
  • 25,172
  • 5
  • 72
  • 123
  • Kudos to [How can I convert the “arguments” object to an array in JavaScript?](http://stackoverflow.com/questions/960866/how-can-i-convert-the-arguments-object-to-an-array-in-javascript) – JDB Apr 13 '15 at 02:44