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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:19:08+00:00 2026-06-02T08:19:08+00:00

Can somebody tell how to "unbind" an anonymous function? In jQuery it’s capable to

  • 0

Can somebody tell how to "unbind" an anonymous function?
In jQuery it’s capable to do that, but how can I implement this Functionality in my own script.

This is the scenario:

The following code attach a onclick event to the Div which have someDivId as ID, now when you click the DIV, it’s showing ‘clicked!’.

var a  = document.getElementById('someDivId');
bindEvent(a,'click',function(){alert('clicked!');});

That’s all great, the problem is how to "un-attach" the Function to the DIV if the function is anonymous or how to "un-attach" all attached events to the ‘a’ Element?

unBind(a,'click'); //Not necessarily the given params, it's just an example.

This is the code for bindEvent Method:

function bindEvent (el,evtType,fn){
    if ( el.attachEvent ) {
        el['e'+evtType+fn] = fn;
        el[evtType+fn] = function(){
            fn.call(el,window.event);
        }
        el.attachEvent( 'on'+evtType, el[evtType+fn] );
    } else {
        el.addEventListener( evtType, fn, false );
    }
}
  • 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-02T08:19:11+00:00Added an answer on June 2, 2026 at 8:19 am

    Finally, and after hours of Test&Errors i have found a solution, maybe it’s not the best or most efficient but… IT WORKS! (Tested on IE9, Firefox 12, Chrome 18)

    First all I’v create two cross-browser and auxiliary addEvent() and removeEvent() methods. (Idea taken from Jquery’s source code!)

    HELPERS.removeEvent = document.removeEventListener ?
    function( type, handle,el ) {
        if ( el.removeEventListener ) {
        //W3C Standard    
        el.removeEventListener( type, handle, true );
        }
    } : 
    function( type, handle,el ) {
        if ( el.detachEvent ) {
            //The IE way
            el.detachEvent( 'on'+type, el[type+handle] );
            el[type+handle] = null;
        }
    };
    
    HELPERS.addEvent = document.addEventListener ?
    function( type, handle,el ) {
        if ( el.addEventListener ) {
            //W3C Standard
            el.addEventListener( type, handle, true );
        }
    } : 
    function( type, handle,el ) {
        if ( el.attachEvent ) {
            //The IE way
            el['e'+type+handle] = handle;
            el[type+handle] = function(){
                handle.call(el,window.event);
            };
            el.attachEvent( 'on'+type, el[type+handle] );
    
        }
    }
    

    Also we need some kind of ‘container’ to store the attached events to elements, like this:

    HELPERS.EVTS = {};
    

    And finally the two callable and exposed to the users Methods:
    The next one to add an Event(event) and associate this Event to a Method (handler) for a specific Element (el).

        function bindEvent(event, handler,el) {
    
                if(!(el in HELPERS.EVT)) {
                    // HELPERS.EVT stores references to nodes
                    HELPERS.EVT[el] = {};
                }
    
                if(!(event in HELPERS.EVT[el])) {
                    // each entry contains another entry for each event type
                    HELPERS.EVT[el][event] = [];
                }
                // capture reference
                HELPERS.EVT[el][event].push([handler, true]);
                //Finally call the aux. Method
                HELPERS.addEvent(event,handler,el);
    
             return;
    
        }
    

    Lastly the method that un-attach every pre-attached events (event) for an specific Element (el)

        function removeAllEvent(event,el) {
    
                if(el in HELPERS.EVT) {
                    var handlers = HELPERS.EVT[el];
                    if(event in handlers) {
                        var eventHandlers = handlers[event];
                        for(var i = eventHandlers.length; i--;) {
                            var handler = eventHandlers[i];
                            HELPERS.removeEvent(event,handler[0],el);
    
                        }
                    }
                }   
    
        return;
    
        }
    

    By the way, to call this methods you must do the following:
    Capture a DOM Node

        var a = document.getElementById('some_id');
    

    Call the method ‘bindEvent()’ with the corresponding parameters.

        bindEvent('click',function(){alert('say hi');},a);
    

    And to de-attach it:

        removeAllEvent('click',a);
    

    That’s all, hope will be useful for somebody one day.

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

Sidebar

Related Questions

Can somebody tell what is the difference between jquery .html() function and innerHTML? <script
i build this really simple script but i did not work. Can somebody tell
Can somebody tell me why compiler thinks that break is necessary after yield return
Can somebody tell me what does this mean? I'm going trough Java book and
can somebody tell me what is jQuery plugin? I have used the jQuery library
Can somebody tell me why this is possible? An private attribute should only be
Specifically, can somebody tell me what is wrong with this piece of code. It
Can somebody tell me what's wrong with that piece of code : NSString* plistPath
can somebody tell me how to use this class timers from python in my
Can somebody tell me how to get mod_rewrite to rename this: our-work-section.php?id=3&title=something to our-work-section/something/3

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.