I've been trying to implement a feature for my website. It is supposed to stop an embed YouTube video when another is run. So I've found a working code somewhere which uses javascript and jquery, tried to play around it, everything works fine but only until I put in on the actual website on wordpress. I suppose the javascript/jquery inserted simply doesn't run but I must admit my javascript knowledge is very limited. My code:
$(function() {
  players = new Array();
  function onYouTubeIframeAPIReady() {
    var temp = $("iframe.yt_players");
    for (var i = 0; i < temp.length; i++) {
      var t = new YT.Player($(temp[i]).attr('id'), {
        events: {
          'onStateChange': onPlayerStateChange
        }
      });
      players.push(t);
    }
  }
  onYouTubeIframeAPIReady();
  function onPlayerStateChange(event) {
    if (event.data == YT.PlayerState.PLAYING) {
      var temp = event.target.a.id;
      var tempPlayers = $("iframe.yt_players");
      for (var i = 0; i < players.length; i++) {
        if (players[i].a.id != temp) players[i].stopVideo();
      }
    }
  }
});<html>
<head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <script type="text/javascript" id="www-widgetapi-script" src="https://s.ytimg.com/yts/jsbin/www-widgetapi-vflS50iB-/www-widgetapi.js" async=""></script>
  <script type="text/javascript" src="https://www.youtube.com/player_api"></script>
</head>
<body>
  <iframe class="yt_players lazy" id=player0 width=385 height=230 src="https://www.youtube.com/embed/erDxb4IkgjM?rel=0&wmode=Opaque&enablejsapi=1;showinfo=0;controls=0" frameborder=0 allowfullscreen data-src="https://www.youtube.com/embed/erDxb4IkgjM?rel=0&wmode=Opaque&enablejsapi=1;showinfo=0;controls=0"></iframe>
  <iframe class="yt_players lazy" id=player1 width=385 height=230 src="https://www.youtube.com/embed/erDxb4IkgjM?rel=0&wmode=Opaque&enablejsapi=1;showinfo=0;controls=0" frameborder=0 allowfullscreen data-src="https://www.youtube.com/embed/erDxb4IkgjM?rel=0&wmode=Opaque&enablejsapi=1;showinfo=0;controls=0"></iframe>
  <iframe class="yt_players lazy" id=player2 width=385 height=230 src="https://www.youtube.com/embed/erDxb4IkgjM?rel=0&wmode=Opaque&enablejsapi=1;showinfo=0;controls=0" frameborder=0 allowfullscreen data-src="https://www.youtube.com/embed/erDxb4IkgjM?rel=0&wmode=Opaque&enablejsapi=1;showinfo=0;controls=0"></iframe>
</body>
</html>What I have checked so far:
- Jquery is working fine.
- I thought the code could be changed somehow due to lazy loading option I use for my website, but it seems like the code remains the same. Same for javascript code.
- I do not see any console errors.
- I tried to put the script in any position possible, nothing changes.
- Tried to wrap in $(function)(), no luck there as well.
- Checked multiple sandboxes/playgrounds, the code is working on every single of them.
What could be a problem here and what else should I try? I appreciate any help!
 
     
    