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?

Mia
  • 6,220
  • 12
  • 47
  • 81

1 Answers1

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);
TAGraves
  • 1,369
  • 1
  • 9
  • 11