Deleting Layers

Question: How do I delete a layer from JavaScript?

Answer: Here's the example that you've already seen in the Creating Layers section:

The buttons in this example were created using the following code:

<form>
<input type=button value="Create layer"
onclick="makeLayer('LYR1',400,250,100,100,'red',1,1)">
<input type=button value="Delete layer"
onclick="deleteLayer('LYR1')">
</form>
To delete a layer, given the layer's
id
string, this code calls the function
deleteLayer(id)
. Here's the JavaScript source code of this function:
function deleteLayer(id) {
 if (document.getElementById
 &&  document.getElementById(id)) {
  var theNode = document.getElementById(id);
  theNode.parentNode.removeChild(theNode);
 }
 else if (document.all && document.all[id]) {
  document.all[id].innerHTML='';
  document.all[id].outerHTML='';
 }
 // OBSOLETE CODE FOR NETSCAPE 4 
 else if (document.layers && document.layers[id]) {
  document.layers[id].visibility='hide';
  delete document.layers[id];
 }
}
See also:

Copyright © 1996-2018, JavascriptFAQ.net.