How to Autoplay a Video Element Using JavaScript

The latest Chrome version has stopped video autoplay if the video is not muted. Firefox and the other browsers will soon follow suit. Here’s a quick way to mute the video and autoplay it when the page has loaded:

function toggleMute(element) {
    element.muted = true;
    element.play()
}

window.addEventListener('load', () => {
    setTimeout(() => {
        toggleMute(document.querySelector('video'));
    }, 1000);
});

That’s it! Enjoy!

on in JavaScript DOM, Methods, Events and Scopes | Last modified on

Related Posts