sketchucation logo sketchucation
    • Login
    🛣️ Road Profile Builder | Generate roads, curbs and pavements easily Download

    JavaScript to Empty Something

    scheduled pinned locked moved Developers' Forum
    4 Posts 3 Posters 255 Views 3 Watching
    loading-more-posts
    • oldest-to-newest
    • newest-to-oldest
    • most-votes
    reply
    • reply-as-topic
    guest-login-reply
    deleted-message
    • 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.

      one-reply-to-this-post last-reply-time 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

        one-reply-to-this-post last-reply-time 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.

          one-reply-to-this-post last-reply-time 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

            one-reply-to-this-post last-reply-time 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