I’m modifying a bit of code I found here from this question. I am trying to get it working now for multiple instances of a video. This is the only way I can get it working but I would like to make it work for each instance that there is a video which changes on each page this applies on.
JavaScript
x
9
1
$( function() {
2
const bgv = $('.bgvid');
3
$('source', bgv).each(function() {
4
const el = $(this);
5
el.attr('src', el.data('src'));
6
});
7
bgv[0].load();
8
bgv[1].load();
9
});
JavaScript
1
8
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
3
<video class="bgvid" playsinline="playsinline" loop="loop" autoplay="autoplay" muted="muted" preload="none">
4
<source type="video/mp4" data-src="https://archive.org/download/UNIVAC-AD-2/UNIVAC2_512kb.mp4">
5
</video>
6
<video class="bgvid" playsinline="playsinline" loop="loop" autoplay="autoplay" muted="muted" preload="none">
7
<source type="video/mp4" data-src="https://archive.org/download/UNIVAC-AD-2/UNIVAC2_512kb.mp4">
8
</video>
I have tried putting it within a loop like this instead of the bgv[0].load(); but it didn’t work:
JavaScript
1
4
1
$('.bgvid').each(function(){
2
$(this).load();
3
});
4
Advertisement
Answer
Just call this.load()
, don’t convert the DOM element into a jQuery element.
JavaScript
1
11
11
1
$( function() {
2
const bgv = $('.bgvid');
3
$('source', bgv).each(function() {
4
const el = $(this);
5
el.attr('src', el.data('src'));
6
});
7
8
bgv.each(function() {
9
this.load();
10
});
11
});
JavaScript
1
8
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
2
3
<video class="bgvid" playsinline="playsinline" loop="loop" autoplay="autoplay" muted="muted" preload="none">
4
<source type="video/mp4" data-src="https://archive.org/download/UNIVAC-AD-2/UNIVAC2_512kb.mp4">
5
</video>
6
<video class="bgvid" playsinline="playsinline" loop="loop" autoplay="autoplay" muted="muted" preload="none">
7
<source type="video/mp4" data-src="https://archive.org/download/UNIVAC-AD-2/UNIVAC2_512kb.mp4">
8
</video>