0

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>';
2
  • you input contentHTML doesn't match with your requirement Commented Feb 16, 2018 at 11:46
  • I think the <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. Commented Feb 16, 2018 at 11:50

2 Answers 2

7

Use replaceWith (comments inline)

var $contentHTML = $( "<div>" + contentHTML + "</div>");

$contentHTML.find( "img" ).each( function(){ //iterate all img elements
  var $parentP = $(this).parent(); //get the parent element
  if ( $parentP[0].nodeName == "P" ) //check if the parent Element is P
  {
     var innerHTML = $parentP.html(); //save the innerHTML value
     $parentP.replaceWith( "<figure>" + innerHTML + "</figure>" ); //replace with figure and retain saved html value
  }
});
console.log($contentHTML.html());

Demo

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>`;

var $contentHTML = $( "<div>" + contentHTML + "</div>");

$contentHTML.find( "img" ).each( function(){
  var $parentP = $(this).parent();
  if ( $parentP[0].nodeName == "P" )
  {
     var innerHTML = $parentP.html();
     $parentP.replaceWith( "<figure>" + innerHTML + "</figure>" );
  }
});
console.log($contentHTML.html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

0

use replace and regular expression:

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>';
var result=contentHTML.replace(/<p([^>]*?)>\s*?<img([\s\S]*?)<\/p>/g,'<figure$1><img$2</figure>');
console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.