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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:49:25+00:00 2026-05-30T08:49:25+00:00

I see that the event handlers registered through .on() are held in $.cache .

  • 0

I see that the event handlers registered through .on() are held in $.cache.
I also see that the event handlers are also held in $(elem).data().

The objects held in $.cache refer to the DOM nodes on which the events are registered. This leads to memory leak when the DOM nodes are detached, and this makes .off() calls mandatory.

I have a situation where I don’t know when the DOM node (to which I attached the event handler) is being detached. While I can hold the reference to that DOM node in my code and call .off() to clean up, it doesn’t seem nice, because it is not straightforward to know when the DOM node is being removed.

What is the best way to do this?

  • 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-30T08:49:26+00:00Added an answer on May 30, 2026 at 8:49 am

    “What is the best way to do this?”

    If you’re going to use jQuery, you must use its API for removing elements, and you must use the proper methods, otherwise, as you stated, you’ll have memory leaks.

    If you don’t know when the DOM node is being removed, and if it is causing a leak, I’d assume this means that you are using another library alongside jQuery. That’s just not a good idea for this very reason.

    You need to ensure that any elements affected by jQuery are removed by jQuery. There’s also some data stored in $.cache that you didn’t explicitly set. This means that all elements should be removed with jQuery, instead of just those that you think may have data.


    “What is the purpose of $.cache in jQuery?”

    To associate handlers and other data with elements. The link between the data and the elements is basically a serial number stored on an expando property on the element.

    If you remove the element without jQuery, the associated data in $.cache is orphaned.

    The purpose of this approach was to prevent potential leaks. Unfortunately it potentially creates more severe leaks.

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

Sidebar

Related Questions

See Also: understanding events and event handlers in C# As a web dev, I
I can see how to attach an event to an object that has no
I see that over on this question LINQy way to check if any objects
I've looked at the Drop and ItemDroppedOn Target event handlers but it appears that
Global Ajax event handlers that are attached with jQuery.ajaxError or jQuery.ajaxComplete don't seem to
In some ASP.NET examples i see that events are used with delegates like this
Recently I discovered, that if I need to see if variable is even (
I see that within MySQL there are Cast() and Convert() functions to create integers
I see that in PL/SQL it is possible to set the session state of
I see that EF can update a model based on an existing database schema.

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.