Only one
by Joshua Koudys
HTML
<iframe class="youtube" width="560" height="315" src="https://www.youtube.com/embed/MepXBJjsNxs" frameborder="0" allowfullscreen></iframe>
<iframe class="youtube" width="560" height="315" src="https://www.youtube.com/embed/O0RU_Nyr4l4" frameborder="0" allowfullscreen></iframe>
JavaScript
document.addEventListener('DOMContentLoaded', function () {
// Needed for keycodes - thanks http://stackoverflow.com/questions/596481/simulate-javascript-key-events
var keyboardEvent = document.createEvent("KeyboardEvent");
var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";
keyboardEvent[initMethod](
"keydown", // event type : keydown, keyup, keypress
true, // bubbles
true, // cancelable
window, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
false, // metaKeyArg
32, // keyCodeArg : unsigned long the virtual key code, else 0
0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
var vids = document.querySelectorAll('.youtube');
var windows = [];
[].forEach.call(vids, function (video) {
// Build window list of iframes
video.addEventListener('mouseenter', function (ev) {
[].forEach.call(vids, function (othervid) {
if (othervid !== video) {
debugger;
video.contentWindow.document.dispatchEvent(keyboardEvent);
}
});
});
});
});