The idea here would be removing the Foreach loop from the code. https://jsfiddle.net/cdost128/
function createResetHandler(player) {
const resetVideo = document.querySelectorAll(".exit");
resetVideo.forEach(function resetVideoHandler(video) {
video.addEventListener("click", function resetVideoHandler() {
player.destroy();
console.log("hit");
}
);
}
)
}
1 option I found that works is this: https://jsfiddle.net/Lvejqks9/
1, 2, 3, 4, 5
player.getIframe()
Can be found in YouTube’s api.
https://developers.google.com/youtube/iframe_api_reference
This method returns the DOM node for the embedded
<iframe>
Also, this line used to be this long: const exitButton = player.getIframe().closest(".inner-container").querySelector(".exit");
I was able to make it shorter.
function createResetHandler(player) {
const exitButton = player.getIframe().closest(".inner-container")
const resetVideo = exitButton.querySelector(".exit");
resetVideo.addEventListener("click", function resetVideoHandler() {
resetVideo.removeEventListener("click", resetVideoHandler);
player.destroy();
console.log("hit");
});
}
2nd option I found that works is this: https://jsfiddle.net/b0729udj/
1, 2, 3, 4, 5
function createResetHandler(player) {
document.addEventListener("click", function({
target
}) {
if (target.closest(".exit")) {
player.destroy();
console.log("hit");
}
}, {
once: true
});
}