I'm building a Wordpress site, where I've made it so that you can insert fullwidth images with parallax through the CKEditor by assigning the inserted images with a class. The script I've written replaces the found images with a div, and uses the source of the replaced image as background for the div. The rest is done by CSS. My problem is that so far I'm only able to get this to work with the first image that has the class.
I know where my code breaks down – it's when I replace the image with the div.
Here is my code:
if (document.getElementsByClassName('fullwidth-image').length > 0) {
let pictures = document.getElementsByClassName('fullwidth-image');
Array.prototype.forEach.call(pictures, function(e){
let imgUrl = e.src,
imgWrapper = document.createElement('div');
imgWrapper.classList.add('fullwidth-container');
imgWrapper.style.backgroundImage = 'url(' + imgUrl + ')';
e.replaceWith(imgWrapper);
});
}
If I comment out
e.replaceWith(imgWrapper);
then
console.log(e);
outputs all the images on the page, as it should. When it's in the code though, only the first image with class="fullwidth-image" gets outputted. How can I write this better?