Search code examples
jqueryimageamp-html

How to change img to amp-img with jQuery?


Accelerated Mobile Pages (AMP) Project

Original

<div class="thumbnail"> 
 <img src="MyImage.jpg" alt="an image"/>
</div>

Result

<div class="thumbnail">
 <amp-img src="MyImage.jpg" width="1080" height="610" layout="responsive" alt="an image"></amp-img>
</div>

How to do it with jQuery ?


Solution

  • Use .replaceWith() API to replace the content :

    $('.thumbnail').find('img').replaceWith(function () {
       return '<amp-img src="'+this.src+'" width="1080" height="610" layout="responsive" alt="'+this.alt+'"></amp-img>'
    });