2

I want to bind a listener to all audio tags (including dynamically added ones) currently on the page that triggers when the user clicks the play button. I was trying it like:

$(document).on("play", "audio", function () {
  alert("here");
});

But it doesn't work. I know I can do it the pure JS way. I could probably do something like:

var audioElements = $('audio')

for(i=0;i<audioElements.length;++i) {
  audioElements[i].addEventListener('play', function(){
   alert("here");
  }
}

But a nice JQuery solution would be preferable. Can't find anything online regarding this.

mickzer
  • 5,958
  • 5
  • 34
  • 57
  • After a lot of attempts, I found that
    $('audio').on("play", function ()
    works if called after the audio element has been loaded. If this happens at page loading this onplay can be added to window.onload otherwise a plugin or a test every tot time must run to check for the newly loaded audio......This is all my help.
    – gaetanoM Dec 16 '15 at 20:34
  • 1
    Possible duplicate of [html5 audio bind timeupdate before element exists](http://stackoverflow.com/questions/29657581/html5-audio-bind-timeupdate-before-element-exists) – mido Dec 17 '15 at 01:31

0 Answers0