You can use querySelector()
to find the image. It would be easier if the image contains an id attribute.
Then, use createElement()
to create the dom element by name amp-img
.
Use setAttribute()
to assign attributes to the new elements such as class, src, width, erc.
Use insertBefore()
to add new element to the page and remove()
to remove the old image element.
NOTE: I am selecting the old image with its src
, please change it as per your need.
var oldImage = document.querySelector('img[src="Mesothelioma_image_1.jpg"]');
var newImage = document.createElement('amp-img');
newImage.setAttribute("class","blog-image");
newImage.setAttribute("src", oldImage.getAttribute('src'));
newImage.setAttribute("width","50");
newImage.setAttribute("height","20");
newImage.setAttribute("layout","responsive");
oldImage.parentNode.insertBefore(newImage, oldImage);
oldImage.parentNode.removeChild(oldImage);
console.log(document.querySelector('amp-img[src="Mesothelioma_image_1.jpg"]')); // to find the appended new image
<img src="Mesothelioma_image_1.jpg"/>