Q:

XMLHttpRequest - get value from URL and write it in a div

I have a div called totalvalue.
<div id="totalvalue"></div>    

I wrote a function to get value from my PHP script (on the same server).
function totalvalue() {
var ajax5 = new XMLHttpRequest();
ajax5.onreadystatechange = function() {
if (ajax5.readyState == 4) {
totalvalue = (ajax5.responseText);
console.log(totalvalue);
document.getElementById("totalvalue").innerHTML = ajax5.responseText;

}
};
ajax5.open("GET", "totalvalue.php", true);
ajax5.send(null);
}

The php script does output a value.
Neither my console nor the div display the output.

A:

This worked for me.
function test5() {

var ajax5 = new XMLHttpRequest();
ajax5.onreadystatechange = function() {
if (ajax5.readyState == 4) {
xxx5 = (ajax5.responseText);
console.log("this is the total value: "+xxx5);

if (xxx5 == 0) {
document.getElementById("totalvalue").innerHTML="Loading...";
} else {
document.getElementById("totalvalue").innerHTML="Total: "+xxx5;
  }

 }
   };
  ajax5.open("GET", "totalvalue.php", true);
   ajax5.send(null);

  }

I presume that where I write the div matter + there could have been an issue with the cache. I cannot tell for sure why the above just started working.

