views:

45

answers:

1

I have multiple HTML5 videos in a slider at http://ghostpool.com/wordpress/phideo and when I click on any of the slider buttons the first video is paused using the following onclick function.

<a href="#" onclick="pausePlayer();">LINK</a>


var htmlPlayer = document.getElementsByTagName('video')[0];

function pausePlayer() {
  htmlPlayer.pause();
}

However the second video does not pause when the slider buttons are clicked. I assume the Javascript above does not work with multiple videos?

EDIT: Additionally, if clicking on the video element itself I want to toggle a play and pause function. I"m using this code, but it plays and stops all videos at once. I assume I'm not looping it correctly:

function togglePlayer() {

for(var i = 0; i < htmlPlayer.length; i++){

    if ( htmlPlayer[i].paused || htmlPlayer[i].ended ) {    

        if ( htmlPlayer[i].ended ) { htmlPlayer[i].currentTime = 0; }
        htmlPlayer[i].play();

      }  else { 

      htmlPlayer[i].pause();

    }

}
}
+1  A: 

This obviously won't work because you are only getting the first element in the array returned by getElementsByTagName. The [0] part accesses the first element in the array. What you would want to do is to iterate through all of them with a loop, like this:

var htmlPlayer = document.getElementsByTagName('video');

function pausePlayer() {
  for(var i = 0; i < htmlPlayer.length; i++){
    htmlPlayer[i].pause();
  }
}

Edit

Regarding your second question - You can try using this instead:

for(var i = 0; i < htmlPlayer.length; i++){
    htmlPlayer[i].onclick = function() {
        if ( this.paused || this.ended ) {    
            if ( this.ended ) { this.currentTime = 0; }
            this.play();
        }  else { 
            this.pause();
        }
    }
}

this in a event handler should refer to the element that called it.

Yi Jiang
@ Yi Jiang That's work brilliantly. Thank you. I have another related issue (original post updated with question also). Essentially, if clicking on the video element itself I want to toggle a play and pause function. I'm using this code (see post), but it plays and stops all videos at once. I assume I'm not looping it correctly.
GhostPool
@ghostpool I've updated my answer, see if it'll work.
Yi Jiang
@Yi Jiang Thanks for the reply. I cannot get this work. Can this work with multiple videos or would I need to use this function for each video? Can I not refer it to the `htmlPlayer` variable as I did with the pause function above?
GhostPool
@ghostpool I've updated by answer again. Instead of using the inline events, you might want to try attaching the event handlers using this method instead. You can't use that variable because it is essentially an array containing all of the videos on that page, so whatever you do with that array you will change all instances of the video.
Yi Jiang
@Yi Jiang: This works. :D However I need to first click the video and then click it again to play, as if the video is not in the right state until the first click is made. Maybe it has something to the with the DOM attributes ` this.paused || this.ended` or the `onclick` event?
GhostPool