I'm using a Learning Management System where users can add YouTube videos, which are then embedded using the iframe tag. They are not embedded responsively automatically. However, I found this JavaScript to wrap the YouTube videos in a div to make them embed responsive:
var embedItem = document.querySelectorAll('iframe[src*="youtube"]');
embedItem.forEach(function(eachEmbed){
let wrapper = document.createElement('div');
wrapper.classList.add('embed-responsive');
wrapper.classList.add('embed-responsive-16by9');
wrapper.appendChild(eachEmbed.cloneNode(true));
eachEmbed.replaceWith(wrapper);
});
This result in a YouTube video being wrapped as follow in HTML:
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="https://www.youtube.com/embed/jTwdtMzZMds" allowfullscreen="true" title="YouTube video">
</iframe>
</div>
This works great, but now, I want to wrap this element in another div <div class="video-wrapper rounded">
Basically the output I want for the original iframe tag is:
<div class="video-wrapper rounded">
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="https://www.youtube.com/embed/jTwdtMzZMds"
allowfullscreen="true" title="YouTube video"></iframe>
</div>
</div>
But I cannot figure out how I can tweak the javascript I found to perform this function automatically. Can anyone adapt the javascript code to perform this function? Does anyone know how to do this?