Expand|Select|Wrap|Line Numbers
- <html>
- <body>
- <div id="div1">div1</div>
- <div id="div2">div2</div>
- <a href=# onclick=check()>delete all div</a>
- <script>
- function check(){
- x = document.getElementsByTagName("div");
- while(x.length>0){
- document.getElementsByTagName("body")[0].removeChild(x[0]);
- }
- }
- </script>
- </body>
- </html>
However, this code will not work:
Expand|Select|Wrap|Line Numbers
- <html>
- <body>
- <img id="img1" src='image1.jpg'>
- <img id="img2" src='image2.jpg'>
- <a href=# onclick=check()>delete all img</a>
- <script>
- function check(){
- x = document.getElementsByTagName("img");
- while(x.length>0){
- document.getElementsByTagName("body")[0].removeChild(x[0]);
- }
- }
- </script>
- </body>
- </html>
Any help appreciated.