0

I have an HTML string as follows (defined in javascript):

<body>
    <div id="stuff">
    bla bla bla
    </div>
    <div id="content">
    Actual content is here.
    </div>
</body>

Above html is my string. I would like to append the div with the id "content" to my actual html file.

How can I read (or parse?) and append a html string like this via jquery?

3
  • from the html string string @MarkC. Commented Nov 1, 2015 at 23:22
  • you want to add another div inside your id="content", am i right ? Commented Nov 2, 2015 at 3:30
  • @JohnReyM.Baylen no, that whole html you see above is my string. I want to (somehow) parse it and add it's div "content" to another html file Commented Nov 2, 2015 at 4:05

1 Answer 1

0

Use jQuery's parseHTML method:

var htmlString = "<body><div id='stuff'>wahwahwah</div><div id='content'>blahblahblah</div></body>"; //your HTML string here.

var parsedHTML= $.parseHTML(htmlString);

var contentDiv = $(parsedHTML).filter('#content');

$('html').append(contentDiv);

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

12 Comments

I used the example above.
The example above won't work since I didn't include a valid HTML string. I'll update with a real example in the next minute.
OK, try what I have now.
I realized that removing the .parseHTML still makes it work
I mean directly using $(htmlString)
|

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.