Categories
JavaScript Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *