localhost
localhost

Reputation: 871

Playing video only when button is clicked

I am trying to play video only when the play button is clicked, using jQuery attr but it doesn't seem to work. Here is a example. I want the video to have sound and play ONLY when button is pressed as i have another button linked to another video. Here is my codepen and my code is following,

jQuery(document).ready(function() {
jQuery(".glyphicon-play-circle").click(function() {
    jQuery(".sports-video").css("display", "block")
    jQuery("#bgvid").toggleClass("is-not-active");   
    jQuery(".fullscreen").toggleClass("is-video-active").attr("autoplay","true");

});

});

Upvotes: 0

Views: 3745

Answers (1)

Daniel
Daniel

Reputation: 555

Codepen

remove the autoplay attribute in the <video>.

To start the video do following in jquery:

$(document).ready(function() {
    $(".glyphicon-play-circle").click(function() {
        $('video').get(0).play();
    });
});

If you want to play several videos on your website you should give the video a class and select the class direct instead of $('video')

Upvotes: 4

Related Questions