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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:13:48+00:00 2026-06-16T03:13:48+00:00

my problem is that I need handle multiple events for rectangle. That sound simple,

  • 0

my problem is that I need handle multiple events for rectangle. That sound simple,
for example this works

  node.click(function(e){
     click(); // this is function defined in same scope, it works ok
  });

  node.mouseout(function(e){
     mouseout();  
  });

But, I want to automatize this, so it should looks like this:

var events = new Array("click", "mouseout");
for(var i in events){
     node[events[i]](function(e){
         events[i](); /*THIS is problem, no matter if it is click or mouseout
                        this always fires function with same name as last item
                        in events array (in this case mouseout) 
                      */
     }
}

Do you have any idea why a how I should solve 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-06-16T03:13:49+00:00Added an answer on June 16, 2026 at 3:13 am

    Your handlers created in a loop are sharing a variable. By the time they are called, the variable is the last value in the loop.

    You have to use a technique I call “freezing your closures” so that each handler gets a separate copy of the shared variable. In your case, the shared variable that changes is i

    Your other problem is that you want to call your functions “click/mouseout” from a string, so you have to get a handle to the function, right now your code is attempting to call "hello"() which does not work

    Your last problems (but not a bug yet) are that you shouldn’t use the Array constructor and you shouldn’t use a for in loop to iterate over arrays.

    function createHandler(eventName) {
        return function(e) {
            window[eventName]();
        }
    }
    
    var events = ["click", "mouseout"];
    for(var i=0; i < events.length; i++){
         node[events[i]](createHandler(events[i]));
    }
    

    The above example is easier to comprehend but you could use self invoking anonymous functions to do the same thing

    var events = ["click", "mouseout"];
    for(var i=0; i < events.length; i++){
         node[events[i]]((function(eventName){
             return function(e) {
                 window[eventName]();
             };
         })(events[i]));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem is this: I have multiple competing threads (100+) that need to access
I bumped into this problem when I created a chart that need a axis
I have this problem that I probably understand but don't know how to handle,
I have an interesting SQL problem that I need help with. Here is the
My specific problem is that I need to execute a (potentially) large number of
I am doing an e-learning application using CakePHP. The problem is that I need
Problem I have some pages that need dynamic data from website to generate output
Problem There are data gaps that need to be filled. Would like to avoid
The problem is that recently on my company we need to make an installer,
Problem I have a UDPlistener application that I need to write a unit test

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.