<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Stop YTPlayer</title>
</head>
<body>
<section id="videoChannels">
<div>
<iframe id="yt0" class="player" src="https://www.youtube.com/embed/8IzxdjVr5ZI?enablejsapi=1" frameborder="0" allowfullscreen></iframe>
<button class='stop' data-id='yt0'>STOP</button>
</div>
<div>
<iframe id="yt1" class="player" src="https://www.youtube.com/embed/AhenpCh6BO4?enablejsapi=1" frameborder="0" allowfullscreen></iframe>
<button class='stop' data-id='yt1'>STOP</button>
</div>
<div>
<iframe id="yt2" class="player" src="https://www.youtube.com/embed/HrmF-mPLybw?enablejsapi=1" frameborder="0" allowfullscreen></iframe>
<button class='stop' data-id='yt2'>STOP</button>
</div>
<div>
<iframe id="yt3" class="player" src="https://www.youtube.com/embed/6KouSwLP_2o?enablejsapi=1" frameborder="0" allowfullscreen></iframe>
<button class='stop' data-id='yt3'>STOP</button>
</div>
</section>
<script>
var vidCh = document.getElementById('videoChannels');
vidCh.addEventListener('click', ytCommand, false);
function ytCommand(event) {
if (event.target != event.currentTarget) {
var btn = event.target.getAttribute('data-id');
var ytp = document.getElementById(btn);
ytp.contentWindow.postMessage('{"event":"command","func":"' + 'stopVideo' + '","args":""}', '*');
}
}
</script>
</body>
</html>
#UPDATE
OP has multiple players and uses plain JavaScript, the following changes are:
1. Multiple players shared the same id, now each player has a unique id.
2. Added a `<button class='stop' data-id='yt*'>STOP</button>` for each player.
- Each button has a `data-id`. It's value corresponds to it's player's id.
3. Wrapped `<section id='videoChannels'></section> around everything.
- Added the `eventListener` to it so when a button is clicked it becomes the `target.currentTarget` initially.
- After the `capture eventPhase` the event chain reaches `event.target` (the button that was clicked) and the function `ytCommand()` is called on the player that corresponds to the button (see step 2.)
##Working Demo
#Stopping YouTube Player
1. Access the iframe by using [`contentWindow`][1] method
2. Next cross-domain communication through an iframe is possible with the [`postMessage` API][2].
3. Next post commands from [YouTube iFrame API][3]
##Relevant Code
$('#stop').on('click', function() {
$('#ytPlayer')[0].contentWindow.postMessage('{"event":"command","func":"' + 'stopVideo' + '","args":""}', '*');
});
##Working Demo
<kbd>[**FIDDLE**][4]</kbd>
[1]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/contentWindow
[2]: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
[3]: https://developers.google.com/youtube/iframe_api_reference#Playback_controls
[4]: https://jsfiddle.net/zer00ne/3fchv2Ld/