Javascript to stop HTML5 video playback on modal window close Javascript to stop HTML5 video playback on modal window close javascript javascript

Javascript to stop HTML5 video playback on modal window close


I'm using the following trick to stop HTML5 video. pause() the video on modal close and set currentTime = 0;

<script>     var video = document.getElementById("myVideoPlayer");     function stopVideo(){          video.pause();          video.currentTime = 0;     }</script>

Now you can use stopVideo() method to stop HTML5 video. Like,

$("#stop").on('click', function(){    stopVideo();});


I searched all over the internet for an answer for this question. none worked for me except this code. Guaranteed. It work perfectly.

$('body').on('hidden.bs.modal', '.modal', function () {$('video').trigger('pause');});


I'm not sure whether ZohoGorganzola's solution is correct; however, you may want to try getting at the element directly rather than trying to invoke a method on the jQuery collection, so instead of

$("#videoContainer").pause();

try

$("#videoContainer")[0].pause();