Play and Pause Video in JavaScript

- 1 minute read

Playing and pausing HTML videos in JavaScript is fairly straightforward.

Consider the following example video element, with an ID of video:

<video id="video" width="300">
    <source src="example.mp4" type="video/mp4">
</video> 

To play or pause a video programmatically, you’ll first need to select the video you want to manipulate from the DOM. In this example, we’ll grab the video with an ID of video:

const video = document.getElementById(`video`);

Once we have our video selected, we can use the built-in play method to play it:

video.play();

Similarly, we can use the built-in pause method to pause it:

video.pause();

Link to this section Conclusion

Working with videos in JavaScript can be quite simple - don’t overcomplicate it!