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 7402071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:38:52+00:00 2026-05-29T04:38:52+00:00

Say I have a container with various elements inside. In turn, any element could

  • 0

Say I have a container with various elements inside. In turn, any element could have various event listeners.

<div id="container">
   <video src="movie.mp4"></video>
   <a>Some button</a>
</div>

With Prototype JS, how do you properly remove each element and their event listeners from memory? The following is the explicit way to clean everything:

$('container').childElements().each(
   function(child) {
      Event.stopObserving(child);
      child.remove();
   }
);

The following is a much quicker way to clear the container:

$('container').update();

But will it cause memory leaks? Will most javascript engines understand that if I clear the innerHTML, I also want to clear the event listeners on each element?

  • 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-29T04:38:54+00:00Added an answer on May 29, 2026 at 4:38 am

    Generally, event handlers will go away when the element goes away. Clearing the innerHTML of the container destroys all contained elements, thus the attached event handlers.

    Older versions of Internet Explorer (<= IE8) have problems re-claiming memory from event handlers when the owning element was destroyed. To accommodate them, you should use Event.stopObserving.

    To be absolutely sure, you could extend Element like this:

    Element.addMethods({
        clear: function(element) {
            element = $(element);
    
            element.descendants().each(function(elem) {
                Event.stopObserving(elem);
            });
    
            return element.update();
        }
    });
    

    This removes all events from all descendants before re-setting the element contents itself. It does not seem like Event.stopObserving() itself is recusive, so without that step there might still be leaks from elements “further down”. Use it like this:

    $('container').clear();
    

    See this as a jsFiddle.

    Disclaimer: I am not familiar with prototype. If the above is incorrect, please leave a comment.

    Related reading:

    • Why does this leak in Internet Explorer 8?
    • Douglas Crockford on memory leaks in IE
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have this: <div class='container'> <img src='image1' id='1' /> <img src='image2' id='2' />
Here is my scenario. I have a container div that has (n) child elements
Say I have a div container with a fixed width and height. Now, I
Let's say I have a container (std::vector) of pointers used by a multi-threaded application.
Let's say I have a sequential container, and a range (pair of iterators) within
In tab container let say I have two tabs [Tab1 & Tab2] Tab1 has
Say I have the following HTML: <head> <style> #container { border: 1px red solid;
Okay, so I have various solutions for vertically aligning elements of arbitrary height within
Let's say I have a class that I don't own: DataBuffer. It provides various
Possible stupid question: let's say I have two apps contained within one project. Can

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.