Custom jQuery event through an object does not fire

I had a problem with a custom jQuery event and listening for it through delegation depending on how the event is fired. A string event fires correctly on both tags. However, the jQuery.Event object only runs when the p tag is directly linked. I prepared a fiddle to demonstrate: http://jsfiddle.net/s35bg/

Here's the HTML:

<p class="delegation">Via delegation:</p>
<p class="direct">Directly:</p>

And JavaScript:

$(function () {
    // bind using delegation
    $(document).on('create', 'p.delegation', function (e) {
        if (e.test) {
            $(this).append(' $.Event');
        } else {
            $(this).append(' string');
        }
    });

    // bind directly
    $('p.direct').on('create', function (e) {
        if (e.test) {
            $(this).append(' $.Event');
        } else {
            $(this).append(' string');
        }
    });

    // trigger the event with a string
    $('*').trigger('create');

    // trigger the event with an object
    var event = $.Event('create');
    event.test = true;
    $('*').trigger(event);
});

Result:

Via delegation: string
Directly: string $.Event

I expect:

Via delegation: string $.Event
Directly: string $.Event

My question is why? Are my expectations wrong or jQuery wrong? Thanks in advance!

+4
source share
1 answer

The last line has changed.

before

var event = $.Event('create');
event.test = true;
$('*').trigger(event);

after

$('*').trigger({type:'create', test:true});
+1
source

All Articles