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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:17:56+00:00 2026-05-17T19:17:56+00:00

I have a predicament: I want to send some data with an event listener

  • 0

I have a predicament: I want to send some data with an event listener but also be able to remove the listener. Here’s the standard closure approach…

var fn = function(e){method(e,data)};
el.addEventListener('click',fn,false);
el.removeEventListener('click',fn,false);

and you could remove the event, just fine. But say, the element was removed from the DOM? Then, you’d be left with the fn function sitting around. After removing a couple thousand DOM elements, it will result in something of a memory leak.

I’ve considered attaching a DOMNodeRemoved event handler, that would remove any left over functions/data along with the removed node. But apparently, that event isn’t cross-browser compatible.

The only other option I’ve come up with would be modifying the element’s DOM. Consider…

el.MyEventData = function(e){method(e,data)};
el.addEventListener('click',el.MyEventData,false);
el.removeEventListener('click',el.MyEventData,false);

Is modifying the DOM acceptable in this situation? The only sticky part of that solution is when you try to add more than one event listener. Let’s say we made a custom function to parse the adding/removing of events…

function makeEvent(fn,data){
    var dataFn = function(e){fn(e,data)};
    //create object to hold all added events
    el.myEvents = {};
    //make ID for this specific event
    var eventID = ranString();
    //add the event to the events object
    el.myEvents[eventID] = [fn,dataFn];
    //finally add the listener
    el.addEventListener('click',dataFn,false);
}
function destroyEvent(fn){
    //find all fn references
    for(var id in el.myEvents){
        if (el.myEvents[id][0] == fn){
            el.removeEventListener('click',el.myEvents[id][1],false);
            el.myEvents[id] = null;
        }
    }
}

It still modifies the DOM, as before, and certainly isn’t a very elegant solution either. Does anyone know of any alternative, better method for passing data?

EDIT: So, I’ve looked into a little of jQuery’s data/event scripts. I don’t completely understand the code, so if someone would clarify, it would be helpful. But it seems as though they use a similar method, by making some type of el.cache property, that holds event data.

  • 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-17T19:17:57+00:00Added an answer on May 17, 2026 at 7:17 pm

    Considering that you use addEventListener this is not an issue as all modern garbage collectors can take care of such situations. The problem with event listeners only exists in IE’s implementation (7-).

    Test – 10 000 addEventListener and remove element (see Windows Task Manager)

    When a DOM object contains a reference
    to a JavaScript object (such an event
    handling function), and when that
    JavaScript object contains a reference
    to that DOM object, then a cyclic
    structure is formed. This is not in
    itself a problem. At such time as
    there are no other references to the
    DOM object and the event handler, then
    the garbage collector (an automatic
    memory resource manager) will reclaim
    them both, allowing their space to be
    reallocated. The JavaScript garbage
    collector understands about cycles and
    is not confused by them.

    http://www.crockford.com/javascript/memory/leak.html

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

Sidebar

Related Questions

My Predicament: I have some things on my server that I want to connect
I just started C++ but have some prior knowledge to other languages (vb awhile
I'm having a little predicament switching between views, here. Alright, so, I have this
I have some JQuery AJAX POSTing data to my backend C# WebForm. It POSTs
Here is my predicament: I have a tableview controller that loads a detail view
Have searched for the answer but no joy, so here goes... I'm working on
Ok I'm kinda in a predicament right now. I have a java program that
have not tested on windows. but in ubuntu when u disconnect from the network,
I have looked everywhere for the answer to this question, but cannot find anything
I have some UI that I need to redraw based on changes to an

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.