Forum Moderators: open

Message Too Old, No Replies

How to remove a page element using removeChild?

removeChild

         

defireman

2:22 am on Sep 30, 2005 (gmt 0)

10+ Year Member



I have tried many times at using removeChild at removing a div from a web page, but without success. I have this as my current DOM structure:

----------------

<body>
<div id="toberemoved">
<a href="[somelink]">Remove DIV</a>
<iframe src="[somepage]">
</iframe>
</div>
...

----------------

If I want to remove the div "toberemoved" from the page, along with all of its children, what should I do with removeChild to remove it? Will this code work (It does nothing when I try to run it)?

function destroydiv() {
var d = document.getElementById("body");
var d_nested = document.getElementById("toberemoved");
var destroyed = d.removeChild(d_nested);
}

Thanks for all the trouble.

birdbrain

3:56 am on Sep 30, 2005 (gmt 0)



Hi there defireman,

try it like this...

<script type="text/javascript">
<!--
function getRidOf(){
document.body.removeChild(document.body.firstChild);
}
//-->
</script>
<body><div id="toberemoved">
<a href="#" onclick=" getRidOf()">Remove DIV</a>
<iframe src="#">
</iframe>
</div>

...Note that for it to work in 'gecko' type browsers <body> and <div> must be positioned as in the code.

birdbrain