Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vanilla javascript: Get a specific element of page in an ajax get request

I'm trying to get #test1 from another page and append it to #test3 of the main page. This is what I have done so far:

<div id="test3"></div>
var request = new XMLHttpRequest();
request.open('GET', '//jsbin.com/wemowe', true);

request.onload = function() {
  if (request.status >= 200 && request.status < 400) {
    var resp = request.responseText;  
    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString(resp,"text/xml");
    var tds = xmlDoc.getElementById("test1");
    console.log(xmlDoc);
    document.getElementById('test3').innerHTML=tds.innerHTML;
  } else {}
};

request.onerror = function() {};

request.send();

Here is JSBin

Any suggestion to make it work?

like image 798
crezc3nt Avatar asked Mar 17 '23 11:03

crezc3nt


1 Answers

The doc type is causing the issue here.

var xmlDoc=parser.parseFromString(resp, "text/xml");

change it to:

var xmlDoc=parser.parseFromString(resp, "text/html");
like image 115
Karthik Avatar answered Mar 23 '23 01:03

Karthik