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 () {
$(document).on('create', 'p.delegation', function (e) {
if (e.test) {
$(this).append(' $.Event');
} else {
$(this).append(' string');
}
});
$('p.direct').on('create', function (e) {
if (e.test) {
$(this).append(' $.Event');
} else {
$(this).append(' string');
}
});
$('*').trigger('create');
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!
source
share