Search code examples
javascripthtmlmp4

HTML how to open a video when i click a button


So I want to make it so when I click the button I have set, the background will change to this mp4 file that will automatically play (with audio), but I'm pretty new to HTML so I cant get the button to actually do anything. This is the code I have so far, it's in the body section. I've tried using onclick with .innerHTML (like in the example below), I've tried different variations of that but I can't get it to work, I can't think of an alternative so I'm asking ya'll beautiful people

<p> Hey, ya like this clock?</p>
<p> Click this button >:)</p>
<input type="button" value="( ͡° ͜ʖ ͡°)" onclick="clicc()">
<script>
function clicc(){
    document.body.innerHTML = "<div class="section">" 
            <video>  
                <source src="ZA WARUDO.mp4" type="video/mp4">  
            </video>
        </div>
}

Solution

  • There's an issue with your use of quotations. you can't create a string with "" and then have HTML attributes with "" as well.

    <html>
    <head>
    <script>
    function clicc(){
     document.body.innerHTML = "<div class='section'><video controls><source src='https://file-examples.com/wp-content/uploads/2017/04/file_example_MP4_480_1_5MG.mp4' type='video/mp4'></video></div>"
    }
    </script>
    </head>
    <body>
    <p> Hey, ya like this clock?</p>
    <p> Click this button >:)</p>
    <input type="button" value="( ͡° ͜ʖ ͡°)" onClick="clicc()">
    </body>
    
    </html>
    

    Check this demo: https://jsfiddle.net/7pruecmh/5/