0

I made a pop-up video button on my website. But when i close it, or click on a modal-out space, the audio still continues.

How can i solve this problem? I tried some javascript function, but i think im doing something wrong...

Thanks!

// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal
btn.onclick = function() {
  modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
      modal.style.display = "none";
  }
}
<button type="button" class="button" id="myBtn">Watch Video</button>
<div id="myModal" class="modal">

<!-- Modal content -->
<div class="modal-content">
  <div class="modal-header">
    <span class="close">×</span>
  </div>
  <div class="modal-body">
<iframe width="560" height="315" src="https://www.youtube.com/embed/QH2-TGUlwu4" frameborder="0" allowfullscreen></iframe>
  </div>
  <div class="modal-footer">
  </div>
</div>

</div>
Stri100
  • 1
  • 1
  • 1

2 Answers2

0

Well the easiest way for you is to re-assign the iframe video src value so that it resets itself and stops. E.g. Assign your iframe an id of example id="yplayer" and then use the following statement where you want to stop and reload the video.

document.getElementById('yplayer').src = document.getElementById('yplayer').src;

The second option is to use the youtube api functions which will involve a little more code handling. Youtube API Docs

Hope this helps.

Nasir T
  • 2,603
  • 1
  • 9
  • 15
0

You should check out the documentation for html audio/video on https://www.w3schools.com/tags/av_met_pause.asp

I suggest you change

span.onclick = function() {
    modal.style.display = "none";
}

with the following code

span.onclick = function stopVideo() {
     document.getElementById('video').pause();
}
Jesper Højer
  • 2,942
  • 1
  • 16
  • 26