I am trying to build a video player that plays the video when the first button is clicked by checking if data-click="0". If yes it sets this to data-click="1". To pause the video I have a second button with the value data-click="1", this pauses the video. But now I have the problem that the first link that plays the video is still set to data-click="1", so I have to click the link twice to play the video. Is it possible to pause the video when clicking the second link and at the same time give the first link data-click="0"?
$(document).ready(function(){
$('.play-pause-btn').on('click',function(){
if($(this).attr('data-click') == 1) {
$(this).attr('data-click', 0)
$('#video')[0].pause();
} else {
$(this).attr('data-click', 1)
$('#video')[0].play();
}
});
});
body {
background-color: #333;
}
.play-pause-btn {
display: block;
background-color: #111;
width: 200px;
margin: 0 auto;
text-align: center;
padding: 5px 0;
font-family: arial;
cursor: pointer;
color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<div style="text-align:center">
<video id="video" width="720">
<source preload="auto" autoplay playsinline controls="controls" src="https://cdn.cuberto.com/cb/video/showreel/1.mp4" type="video/mp4">
<source src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/130527/example-webm.webm" type="video/webm">
</video>
</div>
<div class="play-pause-btn" data-click="0">Should Play the video</div><br><br><br><br>
<div class="play-pause-btn" data-click="1">Should only stop the video</div><br><br><br><br>