ecos301
ecos301

Reputation: 33

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 ?

Upvotes: 1

Views: 1588

Answers (1)

Norlihazmey Ghazali
Norlihazmey Ghazali

Reputation: 9060

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>'
});

Upvotes: 1

Related Questions