user2463374
user2463374

Reputation: 83

Reload div with only javascript without jquery

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

Answers (3)

Aya Sakr
Aya Sakr

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

Jacopofar
Jacopofar

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

Head
Head

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

Related Questions