0

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

2
  • 1
    Why can't you append it to your document and then traverse? Commented Jun 4, 2014 at 3:46
  • Show code you've done so far? Commented Jun 4, 2014 at 3:48

2 Answers 2

2

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.

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

Comments

0

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

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.