Reputation: 1436
I have four divs each with their own video file and button.
.each
and using the index of the element, but I kept running into an issue where .pause() was not a function
https://codepen.io/onlyandrewn/pen/NBvPjx
$(function(){
function playVideo() {
var video = $(".video--inline")[0];
if (video.paused) {
video.play();
video.muted = false;
} else {
video.pause();
video.muted = true;
}
// Loads the clip again, so that it reverts back to the poster image
video.addEventListener("ended", function(){
video.load();
$("button").find("i").removeClass("fa-pause");
$("button").find("i").addClass("fa-play");
});
}
$("button").click(function(){
playVideo();
});
});
<div class="group">
<video src="https://gia.guim.co.uk/2013/10/nsa-decoded/html/asset/video/p1/p1-1.webm" class="video--inline"></video>
<div class="button__wrapper">
<button>Listen</button>
</div>
</div>
<div class="group">
<video src="https://gia.guim.co.uk/2013/10/nsa-decoded/html/asset/video/p1/p1-2.webm" class="video--inline"></video>
<div class="button__wrapper">
<button>Listen</button>
</div>
</div>
<div class="group">
<video src="https://gia.guim.co.uk/2013/10/nsa-decoded/html/asset/video/p1/p1-3.webm" class="video--inline"></video>
<div class="button__wrapper">
<button>Listen</button>
</div>
</div>
<div class="group">
<video src="https://gia.guim.co.uk/2013/10/nsa-decoded/html/asset/video/p1/p1-5.webm" class="video--inline"></video>
<div class="button__wrapper">
<button>Listen</button>
</div>
</div>
Upvotes: 0
Views: 929
Reputation: 18378
Pass click
event to your playVideo
function then target video like this:
var video = $(e.target).closest('.group').find('video')[0];
$(function(){
function playVideo(e) {
var video = $(e.target).closest('.group').find('video')[0];
if (video.paused) {
video.play();
video.muted = false;
} else {
video.pause();
video.muted = true;
}
// Loads the clip again, so that it reverts back to the poster image
video.addEventListener("ended", function(){
video.load();
$("button").find("i").removeClass("fa-pause");
$("button").find("i").addClass("fa-play");
});
}
$("button").click(playVideo);
});
.group {display:inline-block; width:20vw}
video {width:20vw}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="group">
<video src="https://gia.guim.co.uk/2013/10/nsa-decoded/html/asset/video/p1/p1-1.webm" class="video--inline"></video>
<div class="button__wrapper">
<button>Listen</button>
</div>
</div>
<div class="group">
<video src="https://gia.guim.co.uk/2013/10/nsa-decoded/html/asset/video/p1/p1-2.webm" class="video--inline"></video>
<div class="button__wrapper">
<button>Listen</button>
</div>
</div>
<div class="group">
<video src="https://gia.guim.co.uk/2013/10/nsa-decoded/html/asset/video/p1/p1-3.webm" class="video--inline"></video>
<div class="button__wrapper">
<button>Listen</button>
</div>
</div>
<div class="group">
<video src="https://gia.guim.co.uk/2013/10/nsa-decoded/html/asset/video/p1/p1-5.webm" class="video--inline"></video>
<div class="button__wrapper">
<button>Listen</button>
</div>
</div>
Update
We're passing a function name as a parameter to the click handler:
$("button").click(playVideo)
So playVideo
will be called on click and the click Event
object will be passed to playVideo
as a parameter (we called it e
): function playVideo(e)
.
e.target
is the clicked element (button).
Then we're traversing from the button to the corresponding video
.
Upvotes: 4