0

I have 2 iframes which are both embedded Youtube videos. I want to have it so when a user clicks on one of the videos, it will always pause the other video (to ensure both videos are never playing at the same time.)

Below is a sample of my JS

  jQuery( "#video1" ).click(function() {
    console.log("test");
    jQuery('iframe[src*="https://www.youtube.com/embed/"]').each(function(i) {
    this.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
    });
  });

Below is my HTML

<div id="video1" class="col-12 col-md-6 mb-3 mb-md-0">
    <iframe src="https://www.youtube.com/embed/6TI2fdKrT**?&amp;theme=dark&amp;autoplay=1&amp;autohide=2&amp;rel=0&amp;enablejsapi=1" style="width: 100%; height: 270px;"></iframe>
 </div>

<div id="video2" class="col-12 col-md-6 mb-3 mb-md-0">
    <iframe src="https://www.youtube.com/embed/5TI2fdKrT**?&amp;theme=dark&amp;autoplay=1&amp;autohide=2&amp;rel=0&amp;enablejsapi=1" style="width: 100%; height: 270px;"></iframe>
 </div>

The issue I am having is that my click function won't trigger on the #video1 div whilst there is an iframe inside. I tested this by removing the iframe from inside the #video1 div and adding some text and clicking on it and it triggered.

How am I able to get my click function to trigger on the #video1 div whilst there is a iframe inside of it?

Gazz
  • 1,017
  • 3
  • 17
  • 31

0 Answers0