Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
Change Data-Click attribute on second click

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>

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Instead of using a data attribute on the button, you could always just use a variable that isn't tied to a button. I also changed it to isPlaying as that seems more semantically correct.

You can also change the text in the button and just use a single button.

$(document).ready(function() {
  let isPlaying = 0;
  $('.play-pause-btn').on('click', function() {
    if (isPlaying == 1) {
      isPlaying = 0;
      $('#video')[0].pause();
     $(this).html("Should Play the video");
    } else {
      isPlaying = 1;
      $('#video')[0].play();
     $(this).html("Should Pause the video");
    }
  });
});
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 data-playing="0" 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">Should Play the video</div><br><br><br><br>

about 4 years ago · Santiago Trujillo Relatório

0

When you click stop you should reset the play/pause button status back to data-click="0". These should be 2 different buttons

$(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();
    }
  });

  $('.stop-btn').on('click', function() {
    $('#video')[0].pause();
    $('#video')[0].currentTime = 0;
    $('.play-pause-btn').attr('data-click', 0)

  });
});
body {
  background-color: #333;
}

.play-pause-btn,
.stop-btn {
  display: block;
  background-color: #111;
  width: 60px;
  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="stop-btn">Should only stop the video</div><br><br><br><br>

about 4 years ago · Santiago Trujillo Relatório

0

Is it possible to pause the video when clicking the second link and at the same time give the first link data-click="0"?

Yes. There are many ways to go about this, but the example below utilises two functions:

  • activatePlay() // Activates Play and deactivates Pause
  • activatePause() // Activates Pause and deactivates Play

Working Example:

let playPauseButtons = document.getElementsByClassName('play-pause-btn');
let playButton = playPauseButtons[0];
let pauseButton = playPauseButtons[1];

const activatePlay = () => {

  playButton.dataset.state = 1;
  pauseButton.dataset.state = 0; 
}

const activatePause = () => {

  pauseButton.dataset.state = 1;
  playButton.dataset.state = 0; 
}

playButton.addEventListener('click', activatePlay, false);
pauseButton.addEventListener('click', activatePause, false);
.play-pause-btn {
  width: 100px;
  height: 100px;
  font-size: 24px;
  border: 2px solid rgb(191, 191, 191);
  border-radius: 50%;
  cursor: pointer;
}

.play-pause-btn[data-state="0"] {
  color: rgba(127, 127, 127, 0.3);
}

.play-pause-btn[data-state="1"] {
  color: rgba(127, 127, 127, 1);
}
<button type="button" class="play-pause-btn" data-state="0">Play</button>
<button type="button" class="play-pause-btn" data-state="1">Pause</button>

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda