8

With jQuery I can update the contents of a div I using $.load(). How can I update a div in pure JavaScript?

3
  • 2
    Make an ajax call, use .innerHTML to update element contents. Commented Jun 7, 2013 at 14:59
  • in javascript you have to write the function yourself to do the working of $.load .Just google AJAX and you will get how to Commented Jun 7, 2013 at 15:00
  • 1
    developer.mozilla.org/en-US/docs/AJAX/Getting_Started Commented Jun 7, 2013 at 15:01

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

Comments

2

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;

Comments

0

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();

1 Comment

I guess "wrap your div in an iframe" actually means "create a new page with just the div's content and load it via an iframe"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.