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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:57:04+00:00 2026-05-23T22:57:04+00:00

Is it possible to extend the addEvent function in mootools to do something and

  • 0

Is it possible to extend the addEvent function in mootools to do something and also calls the normal addEvent method? Or if someone has a better way to do what I need I’m all years.

I have different ‘click’ handlers depending on which page I’m on the site. Also, there might be more than one on each page. I want to have every click on the page execute a piece of code, besides doing whatever that click listener will do. Adding that two lines on each of the handlers, would be a PITA to say the least, so I thought about overriding the addEvent that every time I add a ‘click’ listener it will create a new function executing the code and then calling the function.

Any idea how I could do it?

  • 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-23T22:57:05+00:00Added an answer on May 23, 2026 at 10:57 pm

    Whereas this is not impossible, it’s a questionable practice–changing mootools internal apis. Unless you are well versed with mootools and follow dev direction on github and know your change won’t break future compatibility, I would recommend against it.

    The way I see it, you have two routes:

    1. make a new Element method via implement that does your logic. eg: Element.addMyEvent that does your thing, then calls the normal element.addEvent after. this is preferable and has no real adverse effects (see above)

    2. change the prototype directly. means you don’t get to refactor any code and it will just work. this can mean others that get to work with your code will have difficulties following it as well as difficulties tracing/troubleshooting- think, somebody who knows mootools and the standard addEvent behaviour won’t even think to check the prototypes if they get problems.

    3. mootools 2.0 coming will likely INVALIDATE method 2 above if mootools moves away from Element.prototype modification in favour of a wrapper (for compatibility with other frameworks). Go back to method 1 🙂

    I think solution 1 is better and obvious.

    as for 2: http://jsfiddle.net/dimitar/aTukP/

    (function() {
        // setup a proxy via the Element prototype.
        var oldProto = Element.prototype.addEvent;
    
        // you really need [Element, Document, Window] but this is fine.
        Element.prototype.addEvent = function(type, fn, internal){
            console.log("added " + type, this); // add new logic here. 'this' == element.
            oldProto.apply(this, arguments);
        };
    
    })();
    
    document.id("foo").addEvent("click", function(e) {
        e.stop();
        console.log("clicked");
        console.log(e);
    });
    

    it is that simple. keep in mind Element.events also should go to document and window. also, this won’t change the Events class mixin, for that you need to refactor Events.addEvent instead.

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

Sidebar

Related Questions

is it possible to extend normal events for plugins in a general way? e.g.
In C#, is it possible to extend a class that has no constructors? Maybe
Is it possible to extend a class which has a decorator applied to it?
Is it possible to extend jQuery 1.3 to include the parseJSON function from 1.4.1+,
is it possible to extend vim functionality via custom extension (preferably, written in Python)?
Is it possible to extend LINQ-to-SQL entity-classes with constructor-methods and in the same go;
Is it possible to extend the SubSonic generator without modifying it's code? I would
Is it possible to extend a base template with another template in Smarty? I
Is it possible to extend user's session expiration time in CI. What I want
Clojure structs can be arbitrarily extended, adding new fields. Is it possible to extend

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.