From this answer for grabbing the player id, and this answer for stopping the video based on the player id, I have tried the following jQuery on a Wordpress site to get the youtube player id and stop the video:
var videoID = player.getVideoData()['video_id'];
$(videoID).get(0).stopVideo();
However, it's not working for me.
Help appreciated.
Edit: this page has two videos that are started by clicking the play buttons at bottom right under Latest Presentations. When the videos are closed, the containing div is simply hidden with CSS, and the video carries on playing in the background.
Edit2:
From zer00ne's answer, I've adapted the JavaScript to become:
jQuery(document).ready(function() {
jQuery('#toggle1').click(function(){
jQuery('#video1').appendTo('body');
jQuery('#video1').addClass('active');
jQuery('#video2').removeClass('active');
});
jQuery('#toggle2').click(function(){
jQuery('#video2').appendTo('body');
jQuery('#video2').addClass('active');
jQuery('#video1').removeClass('active');
});
jQuery('#close1').click(function(){
jQuery('#video1').removeClass('active');
var YT1 = $(this).next('iframe');
YT1[0].contentWindow.postMessage(`{
"event":"command",
"func":"${'stopVideo'}",
"args":""
}`, '*');
});
jQuery('#close2').click(function(){
jQuery('#video2').removeClass('active');
var YT2 = $(this).next('iframe');
YT2[0].contentWindow.postMessage(`{
"event":"command",
"func":"${'stopVideo'}",
"args":""
}`, '*');
});
});
however, this is not stopping the video when I click the X at top right of the video.