Reputation: 56199
How to remove all child nodes from <div id="test"></div>
using Dojo or plain JavaScript?
Upvotes: 11
Views: 39194
Reputation: 3896
You can use the W3C DOM property textContent as a replacement to Microsoft non-standard innerHTML/innerText, it's part of the DOM3 and supported by all major browsers including Internet Explorer since version 9 http://www.w3schools.com/jsref/prop_node_textcontent.asp
Update: innerHTML/innerText is now part of HTML5 standard
Upvotes: 0
Reputation: 14605
dojo.empty(node)
will remove all children from the node, while keeping the node.
dojo.destroy(node)
will remove all children from the node, and then removes the node from its parent as well.
Upvotes: 15
Reputation: 2964
While it is tempting to use el.innerHTML = "", and generally this works, a more correct approach would be:
var el = document.getElementById('test');
while( el.hasChildNodes() ){
el.removeChild(el.lastChild);
}
The reason for this is because IE really hates table manipulation with innerHTML (this is documented somewhere in MSDN).
EDIT: found the MSDN reference: http://msdn.microsoft.com/en-us/library/ms532998%28v=vs.85%29.aspx#TOM_Create
Upvotes: 33