Riad Riad - 1 month ago 4
jQuery Question

How to pass parameters form trigger event to a triggered event in Jquery?

I am trying to pass parameters form 'trigger' event to the triggered event 'click' in Jquery. Jquery documentation says that you can pass external parameters in an array 'extraParameters'. However, I don't know who to receive those parameters in the triggered event. 'click' in my case.

The code that I am using to pass parameters in the trigger event is like the following:

$("#add_docs").trigger("click", [request_id]);


The code that I am using to get the parameters form the 'trigger' event in my 'click' event is like the following:

$("#add_docs").click(params, function () { console.log(params[0]) });


There is wrong with the way that I am trying to get the params. I need a little help here please. Thank you!

Answer

Extra arguments are passed sequantially to the callback, beginning from the second argument (the first is the event object).

$('button').on('click', function(evt, param1, param2) {
    alert(param1+'-'+param2); //"bar-foo"
})

These are then passed as an array via the second param of trigger().

$('button').trigger('click', ['bar', 'foo']);