Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play/pause HTML 5 video using JQuery

People also ask

How do you pause a video in HTML?

HTML Audio/Video DOM pause() Method The pause() method halts (pauses) the currently playing audio or video.

How do I add a play pause button in HTML?

You can use Javascript to perform such actions. var myAudio = document. getElementById("myAudio"); var isPlaying = false; function togglePlay() { if (isPlaying) { myAudio. pause() } else { myAudio.

How do you pause a video when another videos play button is clicked?

onplay = function() { vid2. pause(); };

How do you pause HTML?

HTML | DOM Audio pause() Method The HTML DOM Audio pause() Method is used to pause the currently playing audio. To use the audio pause() method, one must use the controls property to display the audio controls such as play, pause, volume, etc, attached on the audio.


Your solution shows the issue here -- play is not a jQuery function but a function of the DOM element. You therefore need to call it upon the DOM element. You give an example of how to do this with the native DOM functions. The jQuery equivalent -- if you wanted to do this to fit in with an existing jQuery selection -- would be $('#videoId').get(0).play(). (get gets the native DOM element from the jQuery selection.)


You can do

$('video').trigger('play');
$('video').trigger('pause');

This is how I managed to make it work:

jQuery( document ).ready(function($) {
    $('.myHTMLvideo').click(function() {
        this.paused ? this.play() : this.pause();
    });
});

All my HTML5 tags have the class 'myHTMLvideo'


Why do you need to use jQuery? Your proposed solution works, and it's probably faster than constructing a jQuery object.

document.getElementById('videoId').play();