there are several images on a page like this
<img src="https://images4.imagebam.com/qwert.jpg" alt="" />
<img src="https://images4.imagebam.com/werty.jpg" alt="" />
how to use JQuery to change these images to links to these images and add an alt to each image as shown below
<a href="https://images4.imagebam.com/qwert.jpg"><img src="https://images4.imagebam.com/qwert.jpg" alt="image1" /></a>
<a href="https://images4.imagebam.com/werty.jpg"><img src="https://images4.imagebam.com/werty.jpg" alt="image2" /></a>
There is code that does the opposite, but I can’t change it
$("a").each(function() {
$(this).replaceWith("<img src='" + $(this).attr("href") + "'>")
})
2
Answers
You’re just need to reverse the tags in the jQuery. And instead of href get src of image.
Solution :
I had already done a similar action for a website, you can modify your code like this.
This code will allow you to modify all the images that it detects in the code.
Results :
You should get the results as requested.
From
to