Let's say I have those divs with some code in the same document:
<div id="trigger">Click Me!</div>
<div id="handler">Handler</div>
<script type="text/javascript">
$("#handler").on('check', function() {alert('Handler is working!');})
$("#trigger").on('click', function()
{$("#handler").trigger('check');alert('Trigger is working!')
})
</script>
It's working as expected.
However, if I use .load() to inject all this into a page, then only the (native) trigger event, not the (custom) handler event is working.
I guess it has something to do with timing but I thought .on() was supposed to take care of timing issues?
EDIT: THE SOLUTION
I got it all wrong. I wasn't aware that a document loaded simultaneously with the above had its own reference to the Jquery source. So a different version of Jquery was loaded on top of the one already in the DOM. Now it's working without .ready() and even with the original syntax.
Thanx!
$(document).ready(function(){//your code here});which will wait with the execution until the DOM is ready. Otherwise, if you only inject the HTML but not the script then you need to use theon()overload for dynamic bindings alright as mentioned byMash.