How to get a div from another html page with pure js?

1

I want to load a list from a div , from one page to another (both on my server). I know that there is a $('#content').load('paginaexemplo.htm' '#lista') , but I'd like to know how I do this with

asked by anonymous 30.05.2018 / 05:07

1 answer

-1

You can do this with XMLHttpRequest:

var http = new XMLHttpRequest();

var url_ = "pagina.php"; // página de onde virá a div
http.open("GET", url_, true);
http.onreadystatechange = function(){
   if(http.readyState == 4){
      var html = http.responseText;
      // converte o retorno em elementos HTML
      html = new DOMParser().parseFromString(html, "text/html");
      document.getElementById('id_da_div_que_irá_receber').innerHTML = html.getElementById('id_da_div_que_de_onde_virá').innerHTML.trim();
   }
}
http.send(null);
    
30.05.2018 / 05:15