See the following:
$('body').on('whyhellothere', function(){
console.log('done...');
});
$('body').triggerHandler('whyhellothere');
This snippet returns:
done...
While if we reverse the order:
$('body').triggerHandler('whyhellothere');
$('body').on('whyhellothere', function(){
console.log('done...');
});
This snippet returns nothing. Why is this the case?
If you shout in the forest, and then I come along, I won’t hear anything, would I?
You’re registering the event handler after the event was triggered. A registered handler can only listen to events that are triggered after they start listening.
It’s simple physics 😛