Audio Event Does Not Trigger Jquery On Play Event
I try to execute a function on an audio play event : jQuery('audio').on('play', function () { /* ... */ }); But my element is not present at the time this function is executed, s
Solution 1:
Apparently media events( those specifically belonging to audio or video like play
, pause
, timeupdate
, etc) do not get bubbled. you can find the explanation for that in the answer to this question.
So using their solution, I captured the play
event,
jQuery.createEventCapturing(['play']);
jQuery('body').on('play', 'audio', function(){... // now this would work.
JS Bin demo
the code for event capturing( taken from the other SO answer):
jQuery.createEventCapturing = (function () {
var special = jQuery.event.special;
returnfunction (names) {
if (!document.addEventListener) {
return;
}
if (typeof names == 'string') {
names = [names];
}
jQuery.each(names, function (i, name) {
var handler = function (e) {
e = jQuery.event.fix(e);
return jQuery.event.dispatch.call(this, e);
};
special[name] = special[name] || {};
if (special[name].setup || special[name].teardown) {
return;
}
jQuery.extend(special[name], {
setup: function () {
this.addEventListener(name, handler, true);
},
teardown: function () {
this.removeEventListener(name, handler, true);
}
});
});
};
})();
Post a Comment for "Audio Event Does Not Trigger Jquery On Play Event"