Johnny
Johnny

Reputation: 205

HTML / JavaScript Make a video play once a condition is met

For my website, I wanted a specific video to play, once a condition is met.

e.g (If x = 1, play the video)

I have this code in the HTML file:

<div id="video">
    <video width="320" height="240" autoplay,>
    <source src=video_mp4 type="video/mp4">
    <source src=video_ogg type="video/ogg">
    Your browser does not support the video tag.
    </video>
</div>

And in the JavaScript file:

document.getElementById("video").innerHTML.source.src = video_mp4;
document.getElementById("video").innerHTML.source.src = video_ogg;

But it doesn't seem to work.

Upvotes: 0

Views: 1561

Answers (1)

Jalaa Zahwa
Jalaa Zahwa

Reputation: 538

You can use the following :

<video id="video1" src="vid.mp4"></video>
<script>
    function playVideo(){
        var video= document.getElementById("video1");
        video.load();
        video.play();
    }
</script>

Upvotes: 2

Related Questions