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

  • Home
  • SEARCH
  • 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 1046877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:12:53+00:00 2026-05-16T16:12:53+00:00

So I have been adding my events thusly: element.addEvent(‘click’, function() { alert(‘foobar’); }); However,

  • 0

So I have been adding my events thusly:

element.addEvent('click', function() {
    alert('foobar');
});

However, when attempting to remove said event, this syntactically identical code (with “add” switched to “remove”) does not work.

element.removeEvent('click', function() {
    alert('foobar');
});

I assume this is because the two functions defined are not referenced the same, so the event is not technically removed. Alright, so I redefine the event addition and removal:

element.addEvent('click', alert('foobar'));
element.removeEvent('click', alert('foobar'));

Which works great, except now when the page loads, the click event is fired even before it’s clicked!

The function is removed, though, which is great……

  • 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-16T16:12:54+00:00Added an answer on May 16, 2026 at 4:12 pm

    update: when you do .addEvent('type', function(){ }) and .removeEvent('type', function(){ }), even though the functions may have the same ‘signatures’, they are two separte anonymous functions, assigned on the fly. function 1 is !== to function 2 – hence there is no match when MooTools tries to remove it.

    to be able to remove an exact handler, o:

    function handler(){ … }
    el.addEvent(‘click’, handler);
    // .. later
    el.removeEvent(‘click’, handler);

    Internally, events are actually a map of keys to functions in element storage. have a look at this fiddle i did a while back for another SO question – http://www.jsfiddle.net/mVJDr/

    it will check to see how many events are stacked up for a particular event type on any given element (or all events).

    similarly, removeEvent looks for a match in the events storage – have a look on http://jsfiddle.net/dimitar/wLuY3/1/. hence, using named functions like Nikolaus suggested allows you to remove them easily as it provides a match.

    also, you can remove events via element.removeEvents("click") for all click events.

    your page now alerts because you pass on alert as the function as well as execute it with the params ‘foobar’. METHOD followed by () in javascript means RUN THE METHOD PRECEDING IT IMMEDIATELY, NOT LATER. when you bind functions to events, you pass the reference (the method name) only.

    to avoid using an anonymous function and to pass argument,s you can do something like:

    document.id('foobar').addEvent('click', alert.bind(this, 'foo'));
    

    as bind raps it for you, but removing this will be even more complicated.

    as for event delegation, it’s:

    parentEl.addEvents({
        "click:relay(a.linkout)": function(e, el) {
    
        },
        "mouseover:relay(li.menu)": function(e, el) {
    
        }
    });
    

    more on that here http://mootools.net/docs/more/Element/Element.Delegation#Element:removeEvent

    keep in mind it’s not great / very stable. works fine for click stuff, mouseenter is not to be used delegated, just mouseover – which means IE can fire mouseout when it should not. the way i understand it, it’s coming improved in mootools 2.0

    edit updating to show an example of bound and unbound method within a class pattern in mootools

    http://www.jsfiddle.net/wmhgw/

    var foo = new Class({
        message: "hi",
        toElement: function() {
            return this.element = new Element("a", {
                href: "http://www.google.com",
                text: "google",
                events: {
                    "click": this.bar.bind(this), // bind it
                    "mouseenter": this.bar // unbound -> this.element becomes this
                }
            });
    
        },
        bar: function(event) {
            event.stop();
            // hi when bound to class instance (this.message will exist)
            // 'undefined' otherwise.
            console.log(this.message || "undefined");
        }
    });
    
    document.id(new foo()).inject(document.body);
    

    the mouseenter here will be unbound where this will refer to the default scope (i.e the element that triggered the event – the a href). when bound, you can get the element via event.target instead – the event object is always passed on to the function as a parameter.

    btw, this is a slightly less familiar use of class and element relation but it serves my purposes here to illustrate binding in the context of classes.

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

Sidebar

Ask A Question

Stats

  • Questions 522k
  • Answers 522k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The problem was the zero, I had to do this:… May 16, 2026 at 9:27 pm
  • Editorial Team
    Editorial Team added an answer I found some code at http://www.neovolve.com/post/2008/04/07/wcf-security-getting-the-password-of-the-user.aspx that shows how to… May 16, 2026 at 9:27 pm
  • Editorial Team
    Editorial Team added an answer What's with all the lambdas that serve no purpose? Well,… May 16, 2026 at 9:27 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have been adding new elements via jQuery in some of my projects, and
I am working on a server-side framework. Here and there I have been adding
I have been trying to make the FullCalendar events use Facebox to pop up
Total n00b to C# and events although I have been programming for a while.
I have been working on a scheduling website for the past few weeks. I
I have a Google Map, and I am adding event listeners to different things.
I have been able to get my database queries to work properly for deleting
I have been thinking about the best way to calculate the amount of on-hand
I have been programming in python exclusively for 4 years and have never really
Is it possible to have a dynamic number of series for a WPF toolkit

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.