How would I get this part to work with the code?
Changing “var,” to “const ,” “for” to “let”
Adding the iframe part to this:
How would I do that?
Both codes work off the same html:
<div class="video" data-id="M7lc1UVf-VE"></div>
function iframe() {
var iframe = document.createElement("iframe");
var embed = "https://www.youtube.com/embed/ID?autoplay=0";
iframe.setAttribute("src", embed.replace("ID", this.dataset.id));
iframe.setAttribute("frameborder", "0");
iframe.setAttribute("width", "606");
iframe.setAttribute("height", "344");
this.parentNode.replaceChild(iframe, this);
}
This was also with the code, I’m just not using the image stuff:
document.addEventListener("DOMContentLoaded", function() {
var div, n, v = document.getElementsByClassName("youtube");
for (n = 0; n < v.length; n++) {
div = document.createElement("div");
div.setAttribute("data-id", v[n].dataset.id);
/* div.innerHTML = thumb(v[n].dataset.id);*/
div.onclick = iframe;
v[n].appendChild(div);
}
});
I’m not sure which one of these I would be using to do this:
Both are set up differently.
I think the function iframe would be the one I would be using to do this.
function iframe() {
}
1.)
This one is set up like this:
https://jsfiddle.net/zb6mkug3/728/
(function iife() {
/* "use strict";*/
document.addEventListener("DOMContentLoaded", function() {
var div, n, v = document.getElementsByClassName("youtube");
for (n = 0; n < v.length; n++) {
div = document.createElement("div");
div.setAttribute("data-id", v[n].dataset.id);
/*div.innerHTML = thumb(v[n].dataset.id);*/
div.onclick = iframe;
v[n].appendChild(div);
}
});
/*function thumb(id) {
var thumb = '<img src="https://i.imgur.com/AJDZEOX.jpg">',
play = '<div class="play"></div>';
return thumb.replace("ID", id) + play;
}*/
function iframe() {
var iframe = document.createElement("iframe");
var embed = "https://www.youtube.com/embed/ID?autoplay=0";
iframe.setAttribute("src", embed.replace("ID", this.dataset.id));
iframe.setAttribute("frameborder", "0");
iframe.setAttribute("width", "606");
iframe.setAttribute("height", "344");
this.parentNode.replaceChild(iframe, this);
}
}());
2.) This one is set up like this:
https://jsfiddle.net/zb6mkug3/727/
(function iife() {
"use strict";
let youtube = document.querySelector('.youtube');
youtube.addEventListener('click', function() {
const iframe = document.createElement("iframe");
const embed = "https://www.youtube.com/embed/ID?autoplay=0";
iframe.setAttribute("src", embed.replace("ID", this.dataset.id));
iframe.setAttribute("frameborder", "0");
iframe.setAttribute("width", "606");
iframe.setAttribute("height", "344");
this.parentNode.replaceChild(iframe, this);
});
}());