I’m trying to get the videos to play with (“script”) inside the coverClickHandler function.
How would I be able to get it to work?
Doing this allows for the api not to load until it is clicked.
I almost got it working here. Not all the videos play when each is clicked on.
Only 2 players are able to load.
How is that fixed?
https://jsfiddle.net/4ymf9n8h/
(function manageCover() {
function hide(el) {
el.classList.add("hide");
}
function coverClickHandler(evt) {
const cover = evt.currentTarget;
hide(cover);
const tag = document.createElement("script");
tag.src = "https://www.youtube.com/player_api";
const firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
const covers = document.querySelectorAll(".jacket");
covers.forEach(function (cover) {
cover.addEventListener("click", coverClickHandler);
});
}());