sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    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

      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

        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

          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
          • thomthomT Offline
            thomthom
            last edited by

            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
            • First post
              Last post
            Buy SketchPlus
            Buy SUbD
            Buy WrapR
            Buy eBook
            Buy Modelur
            Buy Vertex Tools
            Buy SketchCuisine
            Buy FormFonts

            Advertisement