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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:26:22+00:00 2026-05-27T20:26:22+00:00

This one’s driving me crazy… I have a loop which adds an event listener

  • 0

This one’s driving me crazy… I have a loop which adds an event listener to an SVG object. The object is, for the sake of argument, a small circle, and I have to add mouseover and mouseout events for each of the 10 circles.

My first problem is the standard closure-scope thing – because all the listeners are added in the same loop, they all see the same invalid value of the loop variable. I can fix this, I think, but the second problem is that I have to pass ‘event’ to the listeners, and I can’t find any way to fix both these issues simultaneously.

I’ve tried various versions of this:

for(month = 0; month < nMonths; month++) {
   ...
   shape.addEventListener(
     "mouseover", 
     (function(event, index) { popup_on(event, foo, index); })(event, month),
     false);
   group.appendChild(shape);
}

This particular version gives me ‘event is not defined’. popup_on is the real handler, and must get event and the current value of month. Any idea how I should be doing this? Thanks.

  • 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-27T20:26:22+00:00Added an answer on May 27, 2026 at 8:26 pm

    The event will be passed to your function automatically—just make it the first argument to the function you pass to addEventListener. Also, false is the default value for the capture parameter.

    (function() {
        var i = month;
        shape.addEventListener("mouseover", function(e) { popup_on(e, foo, i); });
    })();
    

    Also, are you ok with foo being closed over in your event callback? If not, you could do the same thing with it

    (function() {
        var i = month;
        var f = foo;
        shape.addEventListener("mouseover", function(e) { popup_on(e, f, i); });
    })();
    

    And if all these local variables are getting annoying, you can make them parameters to possibly make things a bit more tidy

    (function(i, f) {
        shape.addEventListener("mouseover", function(e) { popup_on(e, f, i); });
    })(month, foo);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one is driving me crazy:-) Lets say I have some test xaml code:
This one really has me stumped. I have a documents table which hold info
This one is driving me crazy. AssemblyDefinition asm1 = AssemblyDefinition.ReadAssembly(example); AssemblyDefinition asm2 = AssemblyDefinition.ReadAssembly(example2);
This one has me beat; I have a WPF window with two (important for
This one has us all baffled at work. We have two services running on
This one is weird, I have a page that consists of a html table
This one's a tough one - I have a JFrame that generates JTextFields. When
this one puzzles me. I have an Image with a width of 1600 pixels
This one has been driving me nuts for a few days now and I've
This one's puzzling me. I have a MySQL query, being run though PDO: $stmt

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.