I want to have my video automatically play and loop for me when the user scrolls to a specific area on the video element. I’m aware that I can use a JS observer to do this: however, how do I use JavaScript to add and remove ‘autoplay’?
What I want my default code to be:
JavaScript
x
4
1
<video loop muted>
2
<source src="/videos/my_video.mp4" type="video/mp4" >
3
</video>
4
What I want to happen when the user triggers the JS observer:
JavaScript
1
4
1
<video loop muted autoplay>
2
<source src="/videos/my_video.mp4" type="video/mp4" >
3
</video>
4
Advertisement
Answer
JavaScript
1
2
1
document.querySelector('video').autoplay = true;
2