Myles Gray Myles Gray - 1 year ago 89
HTML Question

How do I clear the contents of a div without innerHTML = "";

I have a div that is filled by JS created DOM elements,

I want the div to be cleared upon the JS function repeating, however I have heard that using

document.getElementById('elName').innerHTML = "";
is not a good idea,

What is a valid alternative to doing this to clear the div's contents?

Answer Source

If you have jQuery then:



var node = document.getElementById('elName');
while (node.hasChildNodes()) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download