parse a document and remove all elements except for one that matches and ID and its children - Javascript DOM

Javascript examples for DOM:Document getElementById

Description

parse a document and remove all elements except for one that matches and ID and its children

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body>
       eee // w  w w  .ja  v  a2  s  . co  m
      <div id="my">
         A
         <div>
            B
         </div>
          C
      </div> 
      <span>D</span> 
      <script>
var saved = document.getElementById('my');
var elms = document.body.childNodes;
while (elms.length) 
   elms[0].parentNode.removeChild(elms[0]);

document.body.appendChild(saved);

      </script>  
   </body>
</html>

Related Tutorials