0

I'm currently using Jquery 1.4.3. I know that there are newer versions but for compatibility with an existing software we have not updated the library yet.

Im trying to get some data that it is passed using the trigger() function. The trigger function has the following params:

.trigger( eventType [, extraParameters ] )

I would like to get those params from the .click() function which says:

.click( handler(eventObject) )

Im not sure how to get those or if I can use trigger to launch an event and some params and using the click listener I can get those. Does someone know if I can use that combination for emitting events and retrieving params?

The way Im trying is like this:

$("#id").trigger('click', ObjectToPass);

and the listener would be defined as follow:

$("#id").click(function(event){...})

What am I missing?

5
  • event.data.yourParamname Commented Jun 5, 2013 at 10:10
  • You are looking for event.data Commented Jun 5, 2013 at 10:10
  • @greut no, event.data is for data that's supplied at event binding time. Commented Jun 5, 2013 at 10:27
  • What's that? What do you mean with supplied at event binding time? Commented Jun 5, 2013 at 10:44
  • @kitimenpolku you can add data when you bind the click handler (i.e. .click(handler, data) which then gets passed as event.data. It's separate to any data sent with .trigger(). Commented Jun 5, 2013 at 12:14

2 Answers 2

5

If you call:

$("#id").trigger('click', ObjectToPass);

then the ObjectToPass is passed as an extra parameter to the event handler:

An array of arguments can also be passed to the .trigger() call, and these parameters will be passed along to the handler as well following the event object

so you can retrieve your data like this:

$("#id").click(function(event, data) {
     // data contains "ObjectToPass"
     ...
})

See http://jsfiddle.net/alnitak/QXZ8K/

NB: the jQuery documentation appears inconsistent. Whilst the text quoted above only talks about arrays, the function specification says "extraParameters: Type: Array or PlainObject"

Passing an object appears to work fine in recent versions of jQuery, but may not work in 1.4.3. In earlier versions you may need to write .trigger(e, [ ObjectToPass ]).

Sign up to request clarification or add additional context in comments.

3 Comments

what's the downvote for?! This is correct, per the documentation (and tested, too!)
documentation link please?
Just discovered this does not work in jquery 1.9+ when triggering click event on checkbox inputs: bugs.jquery.com/ticket/13353
1

Look @greut comment or more solution:

var clickEvent = new $.Event('click');
clickEvent.YourParameter = YourParameterValue;
$("#id").trigger(clickEvent);

and get this parameter in handler:

function(event) { alert(event.YourParameter); }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.