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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:13:59+00:00 2026-06-18T13:13:59+00:00

How do I completely unbind inline javascript events from their HTML elements? I’ve tried:

  • 0

How do I completely unbind inline javascript events from their HTML elements?

I’ve tried:

  • undelegating the event from the body element
  • unbinding the event from the element
  • and even removing the event attribute from the HTML element

To my surprise at least, only removing the onchange attribute (.removeAttr('onchange')) was able to prevent the event from firing again.

<input type="text" onchange="validateString(this)"></input>

I know this is possible with delegates and that’s probably the best way to go, but just play along here. This example is purely hypothetical just for the sake of proposing the question.


So the hypothetical situation is this:

I’m writing a javascript validation library that has javascript events tied to input fields via inline HTML attributes like so:

<input type="text" onchange="validateString(this)"></input>

But, I’d like to make the library a little better by unbinding my events, so that people working with this library in a single-page application don’t have to manage my event handlers and so that they don’t have to clutter their code at all by wiring up input events to functions in my hypothetical validation library… whatever. None of that’s true, but it seems like a decent usecase.

Here’s the “sample” code of Hypothetical Validation Library.js:

http://jsfiddle.net/CoryDanielson/jwTTf/


To test, just type in the textbox and then click elsewhere to fire the change event. Do this with the web inspector open and recording on the Timeline tab. Highlight the region of the timeline that correlates to when you’ve fired the change event (fire the change event multiple times) and you’ll see the event listeners (in the window below) increase by 100 on each change event. If managed & removed properly, each event listener would be properly removed before rendering a new input, but I have not found a way to properly do that with inline javascript events.

What that code does is this:

  1. onChange, the input element triggers a validation function
  2. That function validates the input and colors the border if successful
  3. Then after 1 second (to demonstrate the memory leak) the input element is replaced with identical HTML 100 times in a row without unbinding the change event (because I don’t know how to do that.. that’s the problem here). This simulates changing the view within a single-page app. This creates 100 new eventListeners in the DOM, which is visible through the web inspector.

    • Interesting Note. $('input').removeAttr('onchange'); will actually prevent the onchange event from being fired in the future, but does not garbage collect the eventListener/DOM stuff that is visible in the web inspector.

This screenshot is after change event fires 3 times. Each time, 100 new DOM nodes are rendered with identical HTML and I’ve attempted to unbind the onchange event from each node before replacing the HTML.

enter image description here


Update: I came back to this question and just did a quick little test using the JSFiddle to make sure that the answer was valid. I ran the ‘test’ dozens of times and then waited — sure enough, the GC came through and took care of business.

enter image description here

  • 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-06-18T13:14:01+00:00Added an answer on June 18, 2026 at 1:14 pm

    I don’t think you have anything to worry about. Although the memory can no longer be referenced and will eventually be garbage collected, it still shows up in the Web Inspector memory window. The memory will be garbage collected when the GC decides to garbage collect it (e.g., when the browser is low on memory or after some fixed time). The details are up to the GC implementer. You can verify this by just clicking the “Collect Garbage” button at the bottom of the Web Insepctor window. I’m running Chrome 23 and after I enter text in your validation box about 5 or 6 times, the memory usage comes crashing down, apparently due to garbage collection.

    This phenomenon is not specific to inline events. I saw a similar pattern just by repeatedly allocating a large array and then overwriting the reference to that large array, leaving lots of orphaned memory for GC. Memory ramps up for a while, then the GC kicks in and does its job.

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

Sidebar

Related Questions

The line below is completely failing. template: _.template($('#test').html()), Trying to follow a simple example
I'm completely new to javascript, but I'm trying to create a beta form for
I am trying to unbind an element, an then bind it back after a
I am trying to figure out a way to just use my inline HTML
Completely new to most of this stuff, but basically Im playing around with the
Completely new to asp.net mvc... completely new to web apps so bear with me...
Completely new to java and I have been playing around with regex in a
Not completely a programming question, but its close enough so here goes: In Mac
My application completely on maps which show pins and route information and distance between
I am completely new to HTML5 and have been reading about it for the

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.