Reputation: 83
With jQuery I can update the contents of a div I using $.load()
. How can I update a div in pure JavaScript?
Upvotes: 8
Views: 32387
Reputation: 21
I had radio inputs and wanted to reload them with the default checked values from the html; this worked for me:
const content = document.getElementById("myDiv").innerHTML;
document.getElementById("myDiv").innerHTML = content;
Upvotes: 2
Reputation: 3507
As suggested by Julian H. Lam:
var req = new XMLHttpRequest();
req.open("POST", "address_for_div_content", true);
req.onreadystatechange = function () {
if (req.readyState != 4 || req.status != 200) return;
document.getElementById('id_of_div')=req.responseText;
};
Here the documentation for XMLHttpRequest
Upvotes: 5
Reputation: 568
Pure JavaScript (not JQuery) solution : wrap your div in an iframe, give it an id myFrame, then refresh it from the child like this:
parent.document.getElementById("myFrame").reload();
Upvotes: 0