Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6621055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:11:21+00:00 2026-05-25T21:11:21+00:00

I create many new page elements (in some arbitrary hierarchy) using document.createElement and have

  • 0

I create many new page elements (in some arbitrary hierarchy) using document.createElement and have been using element.removeChild() to erase elements I no longer want. I was wondering if this would correctly clean up all of the sub-elements as well, or if I should be using some sort of recursive function. Javascript uses a garbage collector, so I shouldn’t need to worry about this, right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:11:22+00:00Added an answer on May 25, 2026 at 9:11 pm

    Using element.removeChild(childElement) will remove the nodes from your document tree. If you’ve got a reference to the element, however, the element will not be garbage-collected (GC).

    Consider:
    Example 1 (horrible practice):

    <body><script>
    var d = document.createElement("div");
    document.body.appendChild(d);
    document.body.removeChild(d);
    //The global scope won't disappear. Therefore, the variable d will stay,
    // which is a reference to DIV --> The DIV element won't be GC-ed
    </script></body>
    

    Example 2 (bad practice):

    function foo(){
        var s = document.createElement("span");
        document.body.appendChild(s);
        s.innerHTML = "This could be a huge tree.";
        document.body.addEventListener("click", function(ev){
            alert(eval(prompt("s will still refer to the element:", "s")));
            //Use the provided eval to see that s and s.innerHTML still exist
            // Because this event listener is added in the same scope as where
            // DIV `d` is created.
        }, true);
        document.body.removeChild(s);
    }
    foo();
    

    Example 3 (good practice):

    function main(){
        //Functions defined below
        addDOM();
        addEvent();
        remove();
        //Zero variables have been leaked to this scope.
    
        function addDOM(){
            var d = document.createElement("div");
            d.id = "doc-rob";
            document.body.appendChild(d);
        }
        function addEvent(){
            var e = document.getElementById("doc-rob");
            document.body.addEventListener("click", function(ev){
                e && alert(e.tagName);
                //Displays alert("DIV") if the element exists, else: nothing happens.
            });
        }
        function remove(){
            var f = document.getElementById("doc-rob");
            f.parentNode.removeChild(f);
            alert(f);//f is still defined in this scope
            Function("alert('Typeof f = ' + typeof f);")(); //alert("Typeof f = undefined")
        }
    }
    main();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a little new to drupal but have been using things like devel module
I have an app where I create many uiviews and add them to the
There seem to be many options to create an XML document in .NET. What's
I have a working Rails application on version 2.3.5 - I am using many
I need to create many instances of a specific class in a single page,
This page: http://blog.ostermiller.org/convert-java-outputstream-inputstream describes how to create an InputStream from OutputStream: new ByteArrayInputStream(out.toByteArray()) Other
In many applications the create new record and edit existing record forms are very
Like many others, I'm new to Rails and have a question. I'm working on
I'm trying to create a One-to-many relationship in Django. In my example, I have
I am fairly new to web programming, I have mainly used java to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.