2

I am trying to read the response from an xhr request. I do not want to try to use regex to parse the information. Is there anyway to convert the response string into html elements so I can use document.getElementsById() to read the information I want? The solution cannot use jquery.

Edit: The format is regular html from an xhr response. I am trying to get the information out of divs ie.

<div class="name">John Doe</div>
2
  • It isn't very clear what your response is, but it sounds like you want to fill some element's innerHTML with it Commented May 6, 2011 at 3:26
  • the response is in regular html. I am trying to get information out of divs like this <div class="name">John Doe</div> Commented May 6, 2011 at 3:27

1 Answer 1

2

Sure no problem, if your response is HTML, just create a DIV and shovel your response into it. Not sure about your mention of getElementsByName though, that's not a standard DOM function as far as I know.

var xhrResponse = "<span id='status'>SUCCESS</span>";

var clipboard = document.createElement("DIV");
clipboard.innerHTML = xhrResponse;
var searchingForSomething = clipboard.getElementById('status');
console.log(searchingForSomething.innerHTML); // => SUCCESS

Your temporary DIV never even has to be in the document for this to work.

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

1 Comment

Getting elements by class, however, without jQuery, is verbose. You can do it by searching the whole tree, the simplest code being recursive, or if you only support high-end browsers you can use querySelector.

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.