0

I have this video player code (simplistically)

<div class="videoPlayer">
  <video width="300" height="200">
    <source src="480p.mp4">
  </video>
  <div>...myControls...</div>
</div>

And by this jQuery code I take its events.

$(document).ready(function(){
  $('video').on('canplay',function(){...});
  $('video').on('timeupdate',function(){...});
  $('video').on('progress',function(){...});
  $('video').on('ended',function(){...});
});

Аnd it works. But when I want to add HTML code with jQuery, the following way.

HTML

<div class="myVideo" data-src="video/480p.mp4"></div>

JS

if($(".videoPlayer").length>0){
  $('.videoPlayer').each(function(){
      var th=$(this),
      vidSrc=th.attr('data-src'),
      playerHTML='<div class="videoPlayer videoPlayerNew"><video width="300" height="200"><source src='+vidSrc+'></video><div>...myControls...</div></div>';
      th.replaceWith(playerHTML);
      $(".videoPlayerNew").find("video").load();
      $(".videoPlayerNew").removeClass("videoBlockNew");
  });
}

canplay, timeupdate, progress and ended events no work.

The remaining properties remain working (play, stop, ...).

How can I get these events (canplay, timeupdate, progress, ended).

  • Try [Event binding on dynamically created elements?](https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) .. for simplicity `$(document).on('canplay', 'video' ,function(){...});` – Mohamed-Yousef Jan 13 '18 at 21:35
  • In doing so, even the first method becomes non-working – Vigen Cholakyans Jan 13 '18 at 21:55

0 Answers0