• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

JavaScript to Empty Something

Scheduled Pinned Locked Moved Developers' Forum
4 Posts 3 Posters 233 Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    MartinRinehart
    last edited by 26 Apr 2010, 18:57

    In my tutorial I've this little loop:

    
     while ( div.hasChildNodes() ) { // empty the div
            div.removeChild( div.firstChild );
    
    

    This works, but is it just the hard way to do this:

    
    div.innerHTML = ''
    
    

    ?

    Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

    1 Reply Last reply Reply Quote 0
    • J Offline
      Jim
      last edited by 26 Apr 2010, 19:03

      Completely different things, no? One is removing child nodes from a container element, the other is setting the value of a property of an element.

      Hi

      1 Reply Last reply Reply Quote 0
      • M Offline
        MartinRinehart
        last edited by 26 Apr 2010, 19:27

        They are the same:

        
        <html><body>
        
        <h1> Headline </h1>
        
        <button onclick='dump1()'> Dump1 </button>
        <button onclick='dump2()'> Dump2 </button>
        
        <script>
        
        function dump1() {
            alert( document.body.hasChildNodes() );
            document.body.innerHTML = '';
            alert( document.body.hasChildNodes() );
        }
        
        function dump2() {
            alert( document.body.innerHTML );
            while ( document.body.hasChildNodes() ) {
                document.body.removeChild( document.body.firstChild );
            }
            alert( '[' + document.body.innerHTML + ']' );
        }
        
        </script>
        
        </body></html>
        
        

        Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

        1 Reply Last reply Reply Quote 0
        • T Offline
          thomthom
          last edited by 26 Apr 2010, 20:24

          The former used to be the W3C standard way. The latter used to be the non-standard. But as of HTML5 I believe innerHTML and outerHTML are now standard.
          Pretty well supported anyway.

          Thomas Thomassen — SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          1 / 1
          • First post
            1/4
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement