Categories
JavaScript Answers

How to properly unload or destroy a video element with JavaScript?

Spread the love

Sometimes, we want to properly unload or destroy a video element with JavaScript.

In this article, we’ll look at how to properly unload or destroy a video element with JavaScript.

How to properly unload or destroy a video element with JavaScript?

To properly unload or destroy a video element with JavaScript, we remove the src attribute from the video element.

For instance, we write

const videoElement = document.getElementById("id_of_the_video_element_here");
videoElement.pause();
videoElement.removeAttribute("src");
videoElement.load();

to get the video element with getElementById.

Then we call removeAttribute to remove the src attribute from the video element to unload it.

Conclusion

To properly unload or destroy a video element with JavaScript, we remove the src attribute from the video element.

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 *