0

The goal is to pass parametres through event.data, here is complete example where I tested it : http://jsfiddle.net/

$el.trigger( $.Event('click.NGB', { example: '1' }));
$el.trigger( $.Event('click.NGB', {data: { example: '2' } })); // :-(
$el.trigger( $.Event('click.NGB', [{ example: '3' }]));
$el.trigger( $.Event('click.NGB', [{ data: { example: '4' } }])); 
$el.trigger( $.Event('click.NGB'), { example: '5' });
$el.trigger({ type: 'click.NGB', data: [{ example: '6' }] }); // :-(

I don't know why if an Event object has a data property we can't access it...

But with '.on' function it's works! :-(

$('#response').on( 'click.NGB', {example: '7'}, function(e){ e.data.example; } );  

Anyone know how to do? :-)

ANSWER

After all documentation and tests, I've decided use this method :

$el.trigger('click.NGB', [ {example : '8'} ] );

Thanks to all!

Virgili Garcia
  • 111
  • 1
  • 5
  • take a look here http://stackoverflow.com/questions/16401538/passing-parameters-on-jquery-trigger – RST Aug 22 '14 at 22:49
  • It looks like the data is put in the `Event` object when you create it, but it gets lost by the time it gets to the handler. – Barmar Aug 22 '14 at 22:49
  • 3
    The [documentation](http://api.jquery.com/category/events/event-object/) says: _The following properties are also copied to the event object, though some of their values may be undefined depending on the event:_ and `data` is in that list. – Barmar Aug 22 '14 at 22:50
  • @Barmar thanks! but.. why in function '.on' is posible but not in '.trigger'? – Virgili Garcia Aug 22 '14 at 23:07
  • 1
    Apparently `data` always contains whatever you give as the extra data argument to `.on()`. If you don't give that argument, it's treated as giving `undefined`. That overwrites whatever you specified when creating the `Event` object. – Barmar Aug 22 '14 at 23:11
  • @RST I already looked! look the example 6. is the last comment of linnium from first answer – Virgili Garcia Aug 22 '14 at 23:17

3 Answers3

1

This is a duplicate question of this question.

The "Short Answer" on that question should answer your question quite nicely.

Can trigger() pass data to your event handlers? Yes (as additional parameters)

Can trigger() pass data into the event.data object directly? No (only on() does this)

Community
  • 1
  • 1
Tyler Hoppe
  • 438
  • 3
  • 11
0

If your goal is to send extra data along with the event, you do that with additional parameters, both where you send and receive the event:

Receiving:

// Note discrete param --------------v
$("#foo").on("whatever", function(e, param) {
  display("param: " + JSON.stringify(param));
});

Sending:

// Note discrete param -------v
$("#foo").trigger("whatever", 42);

Live Example

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Fixed here - http://jsfiddle.net/xgf1uhd1/7/

$('#button').on('click', function (e) {
    e.stopPropagation();
    e.preventDefault();

    $el = $('#response');

    $el.trigger( $.Event('click.NGB', { example: '1' }));
});

$('#response').on('click.NGB', function () {
    $ev = arguments[0];
    example = arguments[1];
    $this = $(this);
    $this.find('pre').append('value is === ' + arguments[0].example);
});
Cute_Ninja
  • 4,742
  • 4
  • 39
  • 63