1

I've got a HTML-string I'd like to render but append some HTML after the 2nd paragraph first.

function insertStuff() {
  //var string = "<div><p>paragraph 1</p><p>paragraph 2</p><p>paragraph 3</p></div>"; works
  var string = '<p><img src="http://example.com/my-cool-picture.png" alt="alt text"></p><p>2nd paragraph</p><p>3rd paragrpah</p>' // doesn't work
  var parsedHtml = $(string)
  parsedHtml.find("p:nth-child(2)").after("<p>My cool insert</p>")
  return parsedHtml.html()
}

This works for the HTML string above but the following one only return the <img> after invoking parsedHtml.html()

What am I doing wrong?

2
  • 2
    Unfortunately the .find() method only works for child nodes, therefore you will have to wrap your string with a <div>, use .find() and append content accordingly, and then unwrap it thereafter. Commented Mar 18, 2015 at 12:07
  • 1
    This might help jsfiddle.net/satpalsingh/u6d5m8hn Commented Mar 18, 2015 at 12:08

4 Answers 4

4

Since you are use .html() it will return html of first element.

You can wrap your content in a div like

var parsedHtml = $('<div />').html(string)

Then your code will work.

function insertStuff() {
  var string = '<p><img src="http://example.com/my-cool-picture.png" alt="alt text"></p><p>2nd paragraph</p><p>3rd paragrpah</p>'
  var parsedHtml = $('<div />').html(string)
  parsedHtml.find("p:nth-child(2)").after("<p>My cool insert</p>")
  return parsedHtml.html()
}

alert(insertStuff())
<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

2

Try this

function insertStuff() {
  var string = '<div><p><img src="http://example.com/my-cool-picture.png" alt="alt text"></p><p>2nd paragraph</p><p>3rd paragrpah</p></div>';
  var parsedHtml = $(string)
  parsedHtml.find("p:nth-child(2)").after("<p>My cool insert</p>")
  return parsedHtml.html()
}

You should put this string in a div as parent.

Comments

2

That's because html method as getter returns html content of the first element in the set. You should either wrap all the top-level elements in the set with another element and read it's html content or iterate through the set and concatenate each element's outerHTML property.

parsedHtml.map(function() { return this.outerHTML; }).get().join('');

If you want to get the innerHTML of all the elements in the set, replace outerHTML with innerHTML.

2 Comments

how can I return the content of all elements in the set? I don't want to wrap the content
Thanks for the solution but I think I'll go with wrapping it in a <div
0

when you use find() with a selector it will search inside that selector (in child nodes) that why when you use string with div tag you are getting the desired result and when you delete div the problem occured

1 Comment

use this code function insertStuff() { //var string = "<div><p>paragraph 1</p><p>paragraph 2</p><p>paragraph 3</p></div>"; works var string = '<div><p>test</p><p>2nd paragraph</p><p>3rd paragrpah</p></div>' // doesn't work var parsedHtml = $(string); parsedHtml.find("p:nth-child(2)").after("<p>My cool insert</p>") // return parsedHtml.html() return parsedHtml.html(); }

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.