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

  • Home
  • SEARCH
  • 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 3598234
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:13:01+00:00 2026-05-18T20:13:01+00:00

I create several elements with attached events and delete them with innerHTML = ”.

  • 0

I create several elements with attached events and delete them with “innerHTML = ””. The elements are out, but what about the events ? Are they erased as well ?

<script type = "text/javascript">
    window.onload = function () {
        var container = document.createElement ("div");
            container.style.marginTop = "20px";

        var erase = document.createElement ("input");
            erase.setAttribute ("type", "button");
            erase.value = "Erase inputs";

        var insert = document.createElement ("input");
            insert.setAttribute ("type", "button");
            insert.value = "Insert inputs";

        document.body.appendChild (insert);
        document.body.appendChild (erase);
        document.body.appendChild (container);

        erase.onclick = function () {
            if (container.innerHTML !== "") {
                container.innerHTML = "";

                alert ("innerHTML = \"\". What about the events ?")
            }
        }

        insert.onclick = function () {
            for (var i = 0; i < 1000; i ++) {
                var input = document.createElement ("input");
                    input.setAttribute ("type", "button");
                    input.value = "Click Me !";

                input.onclick = function () {
                    alert ("STOP ! Hammer time :)")
                }

                container.appendChild (input);
            }
        }
    }
</script>

Possible memory leak ?

Thanks.

  • 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-18T20:13:01+00:00Added an answer on May 18, 2026 at 8:13 pm

    No – to be safe, set your “onclick” attributes to null before you blow away the contents.

        erase.onclick = function () {
            if (container.innerHTML !== "") {
                var inps = container.getElementsByTagName('input');
                for (var inpi = 0; inpi < inps.length; ++inpi)
                  inps[i].onclick = null; // also "onchange", "onblur", anything else
    
                container.innerHTML = "";
    
                alert ("innerHTML = \"\". What about the events ?")
            }
        }
    

    The DOM node garbage collector in Internet Explorer doesn’t know what to do with JavaScript garbage, so it’s lost. (It may well be the case that DOM nodes dangling off free JavaScript objects are similarly ignored; I can’t recall exactly.)

    Each one of those event handlers will have a reference to the closures formed by the outer “onload” handler and by the “onclick” handler of the “insert” control. They’ll all be sharing a reference, so in this particular case I don’t think it’d be that much memory. If, however, you set up each of those 1000 event handlers via another function, then there’d be that much more leakage.

    I hate to say this, but one of the conveniences of using a framework is that a lot (not all but a lot) of this sort of thing is taken care of for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several web services I need to create for them some testing mechanism
I have several input and option elements on my page, each (well almost) have
I know there are several questions named like this, but they don't seem to
Trying to create several layers of folders at once C:\pie\applepie\recipies\ without using several different
I need to create several applications that all share a Microsoft SQL Server database.
I'd like to create several modules that will be used in nearly all scripts
What is the best way to create several scrollable regions in an ActionScript 3
In one of my VB6 forms, I create several other Form objects and store
I need to create simple reusable javascript object publishing several methods and parameterized constructor.
I have an ABC with several derived classes. To create these derived classes I

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.