user3159432
user3159432

Reputation: 1

Convert htmlString to Dom object in Javascript

I made a http request and received a htmlString, now I want to convert it to Dom object to query its elements. Thanks for you help

Upvotes: 0

Views: 41

Answers (2)

Mr. Smee
Mr. Smee

Reputation: 960

using jQuery you could do something like this:

  var yourStringFromServer = '<div><div id="helloWrap"></div></div>';
  var a = $(yourStringFromServer); // create new jQuery instance with string
  a.find('#helloWrap').html('hello'); // find  the helloWrap node and set html
  a.appendTo('body'); // append html to body

Upvotes: 0

jfriend00
jfriend00

Reputation: 708036

You can create a container object (I've used a div here) and then assign your html string to .innerHTML and then you can query the child objects that are created.

var container = document.createElement("div");
container.innerHTML = htmlString;

The child nodes of the container object are what is created from your HTML.

Upvotes: 2

Related Questions