Fernando YP Fernando YP - 3 months ago 17
HTML Question

How to change inner html of an <img> tag?

This is being output:

<img width="1080" height="1080" src="/image.jpg" class="post-image" alt="image" />


I need to change it to this: (
src
data
)

<img width="1080" height="1080" data="/image.jpg" class="post-image" alt="image" />

Answer

just use the simple JQ below :

first get the src then add it as data , then remove the src

var src = $("img").attr("src")
$("img").attr("data", src).removeAttr("src")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img width="1080" height="1080" src="/image.jpg" class="post-image" alt="image" />