0

im trying to get a element from the url html file. element <td>Thu Dec 18 10:33:19 EST 2014</td> which is in the url html page that is passed into loadHTML function. which sends the html page string to another function called handler. from the parser is it possible to get the specific .

function loadHTML(spanId, url) {
var xmlhttp = new XMLHttpRequest();


xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 /* complete */) {
                handler(xmlhttp.responseText, spanId);
          }
};
xmlhttp.send();
}


function handler(responseText, spanId) {
var parser =new DOMParser();
parser.getDocument()
//get the date/time from parser 
var dateTime = …
//get class name from parser
var className = …

var span = document.getElementById(spanId);
span.innerHTML = dateTime;
span.className = className;
}

2 Answers 2

2

Think you need to do it like this:

parser = new DOMParser();
var doc = parser.parseFromString(responseText, "text/html");

var dateTime = doc.querySelector("td.dateTime").innerHTML;

and set class="dateTime" on <td>

From MDN on DOMParser

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

1 Comment

or get a specific string from doc to dateTime
2

In modern browsers you can also set xhr.responseType = "document" and directly get a document as result from the XHR.

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.