I have HTML string which contains various elements. Consider following is the HTML string -
var contentHTML = '<p>Some random string</p><p><img src="xyz.jpg" style="width: 100%;"><span some style></span></p><p><span style="font-size: 16.8px;"><b>Ingredients</b></span></p><p>Again some string</p><p><img src="xyz.jpg" style="width: 100%;"></p>';
My requirement is to get tag before and after each img tag. If tag before img tag is p tag, then replace it with <figure> and closing </p> with </figure>.
I tried looping through img tag and able to get image details perfectly.
How can replace elements before and after image.
$(this.content).find('img').each(function() {
console.log($(this).prev());
console.log($(this).attr('src'));
});
String I require -
var contentHTML = '<p>Some random string</p><figure><img src="xyz.jpg" style="width: 100%;"><span some style></span></figure><p><span style="font-size: 16.8px;"><b>Ingredients</b></span></p><p>Again some string</p><figure><img src="xyz.jpg" style="width: 100%;"></figure>';
<p>and</p>are meant that surround the<img>, although I agree that the wording is a but ambiguous. Also I wonder what happened to the<span some style></span>that directly follows the<img>in the example.