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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.