I created a modal that opens when clicking on an image, in this modal will have a video of youtube and a small description of the side, however whenever I close the modal the video continues to reproduce. I wanted the video to pause when I clicked close and (if possible) started to play by itself when opening the modal. I have tried several Java code but it seems that for some reason none works with my code.
My code is as follows:
<script>
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '365,99',
width: '650',
videoId: 'd0D8tTtY4lU',
});
$(".text").on("click", function() {
player.playVideo();
});
}
</script>
</head>
<body>
<a href="#abrirmodal"><div class="container"><img class="image"
src="https://cdn.flickeringmyth.com/wp-content/uploads/2017/05/Paddington-2-posters-1-header.jpg" alt="Paddington 2 - Trailer" ><div class="middle">
<div class="text"><img src="play.png" alt="play" width="60" height="60">
</div>
</div></div></a>
<div id="abrirmodal" class="modalDialog">
<div>
<table border="1">
<a href="#fechar" title="Fechar" class="fechar">x</a>
<tr>
<td ROWSPAN="2"> <div id="player"></div>
</div> </td>
<td><h2>Modal</h2></td></tr>
<tr><td>Esta é uma simples janela de modal.
Você pode fazer qualquer coisa aqui, página de Login, pop-ups, ou
formulários.</td></tr>
</table>
</div>
</div>
</body>
I've tried using the YouTube iframe link instead of the API, but I have not been able to either. The function:
$(".text").on("click", function() {
player.playVideo();
});
It also does not seem to work, either to start playback or to pause.