How to stop HTML5 video playback on modal window close with JavaScript?

Sometimes, we want to stop HTML5 video playback on modal window close with JavaScript.

In this article, we’ll look at how to stop HTML5 video playback on modal window close with JavaScript.

How to stop HTML5 video playback on modal window close with JavaScript?

To stop HTML5 video playback on modal window close with JavaScript, we call the pause method.

For instance, we write

const video = document.getElementById("myVideoPlayer");
const stopVideo = () => {
  video.pause();
  video.currentTime = 0;
};

to select the video element with getElementById.

Then in the stopVideo function, we call video.pause to pause the video.

And we set its currentTime to 0 seconds to move the video back to the beginning.

Conclusion

To stop HTML5 video playback on modal window close with JavaScript, we call the pause method.