1

I am using jQuerys .on() to bind event handlers to a video element. And everything works great: http://jsfiddle.net/me2loveit2/r0zt4qLg/3/

$('#testid').on('pause play ended', function (e) {
    $('body').append('<br>Event triggered: ' + e.type);
});

But if the video does not initially exist, the events are not firing: http://jsfiddle.net/me2loveit2/r0zt4qLg/5/

Which makes sense because according to the jQuery documentation on .on() : http://api.jquery.com/on/ It states:

Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on().

and it goes on to saying to use delegated events,

but as in my example(http://jsfiddle.net/me2loveit2/r0zt4qLg/5/) the parent element will never fire those events, because they do not apply there. Is there a way to make this work or is the only solution to attach the event handlers once the element exists?

Philipp Werminghausen
  • 1,142
  • 11
  • 29

1 Answers1

1

you have delegate the events like this:

$(document).on('pause play ended','#testid', function (e) {
    $('body').append('<br>Event triggered: ' + e.type);
});
Amin Jafari
  • 7,157
  • 2
  • 18
  • 43