Sometimes you need to make your YouTube video automatically disappear immediately after it finishes playing.

I wanted to know how to do this because I needed the functionality for one of our quiz pages. In this example, I wanted to autoplay a YouTube video in which I encourage visitors to take the quiz, and then make the video disappear, so that the visitor can actually take the darn quiz without a thumbnail of my face hovering above it and distracting them.

vid - How to automatically hide a YouTube video after it finishes playing

After beating my head against the wall for 45 minutes and still not getting it to work, I asked one of our awesome MemberFix techs, Cristian, to help me out. Naturally, he got it working (thanks Cristian!) Here’s how you can make your YouTube video disappearing after it’s done playing:

1 – Copy the code from the snippet below and paste it into your TEXT editor if you’re using WordPress. (I recommend you click on the DOWNLOAD RAW link on the following snippet to get the code in plain text.)

*Note: If you’re using some kind of visual composer or page builder (for example, we use Divi for almost everything we do), then make sure you create a “code” element, since the snippet you’re pasting contains Javascript.

divi code module - How to automatically hide a YouTube video after it finishes playing

2 – Find line 15 of the code snippet above and replace the video ID with YOUR YouTube video ID.

You can find this by opening your video in YouTube and looking at the string of characters at the end of the URL (the part that comes immediately after the

video id - How to automatically hide a YouTube video after it finishes playing

3 – On lines 12 and 13 change the height and width dimensions to whatever you prefer.

4 – On line 14 you can toggle several additional options (0 = false; 1 = true):

playerVars: { ‘rel’:0, ‘autoplay’: 1, ‘controls’: 0 },

rel means “related videos”. If you want related videos to show after the video plays then set this to ‘1’. If not, set it to ‘0’. Since the YouTube player will disappear after the video completes this option doesn’t much matter anyway.

autoplay means “automatically play this video when the page loads”. Set this to ‘0’ if you don’t want the video to autoplay. Set it to ‘1’ if you do.

controls refers to the video player controls (stop, pause, play, etc.) If you want the controls to be visible set this to ‘1’; if you want them to be hidden set it to ‘0’.

5 – Save your work and test it!

If you did everything correctly the video should disappear after it finishes playing, like this:

<div id="player"></div><!-- [et_pb_line_break_holder] --><script><!-- [et_pb_line_break_holder] -->var tag = document.createElement('script');<!-- [et_pb_line_break_holder] -->tag.src = "";<!-- [et_pb_line_break_holder] -->var firstScriptTag = document.getElementsByTagName('script')[0];<!-- [et_pb_line_break_holder] -->firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);<!-- [et_pb_line_break_holder] --><!-- [et_pb_line_break_holder] -->var player;<!-- [et_pb_line_break_holder] --><!-- [et_pb_line_break_holder] -->function onYouTubeIframeAPIReady() {<!-- [et_pb_line_break_holder] --> player = new YT.Player('player', {<!-- [et_pb_line_break_holder] --> height: '219',<!-- [et_pb_line_break_holder] --> width: '386',<!-- [et_pb_line_break_holder] --> playerVars: { 'rel':0, 'autoplay': 0, 'controls': 1 },<!-- [et_pb_line_break_holder] --> videoId: 'UmCGnYc9SCE',<!-- [et_pb_line_break_holder] --> events: {<!-- [et_pb_line_break_holder] --> 'onReady': onPlayerReady,<!-- [et_pb_line_break_holder] --> 'onStateChange': onPlayerStateChange<!-- [et_pb_line_break_holder] --> }<!-- [et_pb_line_break_holder] --> });<!-- [et_pb_line_break_holder] -->}<!-- [et_pb_line_break_holder] --><!-- [et_pb_line_break_holder] -->function onPlayerReady(event) {<!-- [et_pb_line_break_holder] --> //;<!-- [et_pb_line_break_holder] -->}<!-- [et_pb_line_break_holder] --><!-- [et_pb_line_break_holder] -->var done = false;<!-- [et_pb_line_break_holder] --><!-- [et_pb_line_break_holder] -->function onPlayerStateChange(event) { <!-- [et_pb_line_break_holder] --> if( === YT.PlayerState.ENDED) { <!-- [et_pb_line_break_holder] --> stopVideo();<!-- [et_pb_line_break_holder] --> }<!-- [et_pb_line_break_holder] -->}<!-- [et_pb_line_break_holder] --><!-- [et_pb_line_break_holder] -->function stopVideo() {<!-- [et_pb_line_break_holder] --> player.stopVideo();<!-- [et_pb_line_break_holder] --> document.getElementById("player").style.display = "none";<!-- [et_pb_line_break_holder] --> document.getElementsByClassName('fluid-width-video-wrapper').style.padding = "0"; <!-- [et_pb_line_break_holder] -->}<!-- [et_pb_line_break_holder] --></script>