11
// now pause all active videos
$('.vid').get(0).pause();
$('.vid').get(1).pause();
$('.vid').get(2).pause();

That's what I have right now, but it's less than ideal. I'm a bit tired, so I could be missing something obvious, but how can I just tell it do pause all instances of .vid class on the page?

prismspecs
  • 1,482
  • 6
  • 20
  • 35

7 Answers7

21

Try

$('.vid').each(function() {
    $(this).get(0).pause();
});
n8wrl
  • 19,439
  • 4
  • 63
  • 103
14

There's no real need for jQuery, the ES6 way (that should work in most modern browsers...)

For all <video> tags:

document.querySelectorAll('video').forEach(vid => vid.pause());

or in your case (looking for the .vid class):

document.querySelectorAll('.vid').forEach(vid => vid.pause());
bnjmn
  • 139
  • 2
  • 5
6
$("video").each(function() {
    $(this).get(0).pause();
});

more general? for all videos!

2

Video.js stores all the players on the page in an Object in V.players so you could do the below.

Video JS 3.x

Normal JS

for( player in window._V_.players ) {
    window._V_.players[player].pause();
}

http://jsfiddle.net/3n1gm4/SNZAS/

jQuery

jQuery.each( window._V_.players, function( i, player ) {
    player.pause();
});

http://jsfiddle.net/3n1gm4/cJ8jx/

Video JS 4.x

Normal JS

for( player in window.vjs.players ) {
    window.vjs.players[player].pause();
}

jQuery

jQuery.each( window.vjs.players, function( i, player ) {
    player.pause();
});
tastybytes
  • 1,309
  • 7
  • 17
0

would

jQuery.each($('.vid'), pause)

work?

j_mcnally
  • 6,928
  • 2
  • 31
  • 46
0

This option works in all browsers. It is a simple for loop to get all the video tags, then pauses each of them.

var videoList = document.getElementsByTagName("video");
for (var i = 0; i < videoList.length; i++) {
    videoList[i].pause();
}
Genspec
  • 2,279
  • 2
  • 14
  • 10
0

hey take a look on working live example here:

$('video').each(function() {
    $(this).get(0).pause();
});

https://codepen.io/abdulrehman25/pen/xxaXvoV

Abdul Rehman
  • 142
  • 1
  • 12