Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shahr's avatar
Level 10

Uncaught TypeError: video is undefined

When a video is played. This mask should disappear when I play the video. that can't be I think it can be done with javascript. How?

blade

<div class="container-fluid background-video">
    <video poster="{{ asset('storage/'.$setting->image) }}" class="w-100" controls>
        <source src="{{ asset('storage/'.$setting->video)  }}" type="video/mp4">
        <source src="{{ asset('storage/'.$setting->video)  }}" type="video/ogg">
        <source src="{{ asset('storage/'.$setting->video)  }}" type="video/webm">
        <object data="{{ asset('storage/'.$setting->video)  }}">
            <embed src="{{ asset('storage/'.$setting->video)  }}">
        </object> 
    </video>
</div>

style

.background-video {
    position: relative;
    overflow: hidden;
    width: 100%;
    height: 0;
    padding-bottom: 56.25%; /* This creates a 16:9 aspect ratio container for the video */
    background-color: #000; /* Set background color to black */
}
.background-video video {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    opacity: 0.8; /* Change the opacity as per your requirement */
    mask-image: linear-gradient(to bottom, rgba(0,0,0,1), rgba(0,0,0,0)); /* This applies a gradient mask to fade out the edges of the video */
}

script

<script>
    const mask = document.getElementsByClassName("background-video")[0];
    const video = document.getElementsByTagName("background-video")[0];

    video.addEventListener('play', function() {
        mask.style.display = "none";
    });
</script>
0 likes
4 replies
LaryAI's avatar
Level 58

The issue is with the following line in the script:

const video = document.getElementsByTagName("background-video")[0];

getElementsByTagName returns a collection of elements with the given tag name. In this case, it should be video instead of background-video. So, the correct line should be:

const video = document.getElementsByTagName("video")[0];

This should fix the "Uncaught TypeError: video is undefined" error.

Updated script:

<script>
    const mask = document.getElementsByClassName("background-video")[0];
    const video = document.getElementsByTagName("video")[0];

    video.addEventListener('play', function() {
        mask.style.display = "none";
    });
</script>

Please or to participate in this conversation.