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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:13:26+00:00 2026-05-12T06:13:26+00:00

The Objective I want to dynamically assign event handlers to some divs on pages

  • 0

The Objective

I want to dynamically assign event handlers to some divs on pages throughout a site.

My Method

Im using jQuery to bind anonymous functions as handlers for selected div events.

The Problem

The code iterates an array of div names and associated urls. The div name is used to set the binding target i.e. attach this event handler to this div event.

While the event handlers are successfully bound to each of the div events, the actions triggered by those event handlers only ever target the last item in the array.

So the idea is that if the user mouses over a given div, it should run a slide-out animation for that div. But instead, mousing over div1 (rangeTabAll) triggers a slide-out animation for div4 (rangeTabThm). The same is true for divs 2, 3, etc. The order is unimportant. Change the array elements around and events will always target the last element in the array, div4.

My Code – (Uses jQuery)

var curTab, curDiv;
var inlineRangeNavUrls=[['rangeTabAll','range_all.html'],['rangeTabRem','range_remedial.html'],
                ['rangeTabGym','range_gym.html'],['rangeTabThm','range_thermal.html']];
        for (var i=0;i<inlineRangeNavUrls.length;i++)
        {
            curTab=(inlineRangeNavUrls[i][0]).toString();
            curDiv='#' + curTab;
            if  ($(curDiv).length)
            {
                $(curDiv).bind("mouseover", function(){showHideRangeSlidingTabs(curTab, true);} );
                $(curDiv).bind("mouseout", function(){showHideRangeSlidingTabs(curTab, false);} );
            }
        }

My Theory

I’m either not seeing a blindingly obvious syntax error or its a pass by reference problem.
Initially i had the following statement to set the value of curTab:

curTab=inlineRangeNavUrls[i][0];

So when the problem occured i figured that as i changed (via for loop iteration) the reference to curTab, i was in fact changing the reference for all previous anonymous function event handlers to the new curTab value as well…. which is why event handlers always targeted the last div.

So what i really needed to do was pass the curTab value to the anonymous function event handlers not the curTab object reference.

So i thought:

curTab=(inlineRangeNavUrls[i][0]).toString();

would fix the problem, but it doesn’t. Same deal. So clearly im missing some key, and probably very basic, knowledge regarding the problem. 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-12T06:13:26+00:00Added an answer on May 12, 2026 at 6:13 am

    You need to create a new variable on each pass through the loop, so that it’ll get captured in the closures you’re creating for the event handlers.

    However, merely moving the variable declaration into the loop won’t accomplish this, because JavaScript doesn’t introduce a new scope for arbitrary blocks.

    One easy way to force the introduction of a new scope is to use another anonymous function:

    for (var i=0;i<inlineRangeNavUrls.length;i++)
    {
      curDiv='#' + inlineRangeNavUrls[i][1];
      if ($(curDiv).length)
      {
        (function(curTab)
        {
          $(curDiv).bind("mouseover", function(){showHideRangeSlidingTabs(curTab, true);} );
          $(curDiv).bind("mouseout", function(){showHideRangeSlidingTabs(curTab, false);} );
        })(inlineRangeNavUrls[i][0]); // pass as argument to anonymous function - this will introduce a new scope
      }
    }
    

    As Jason suggests, you can actually clean this up quite a bit using jQuery’s built-in hover() function:

    for (var i=0;i<inlineRangeNavUrls.length;i++)
    {
      (function(curTab) // introduce a new scope
      {
      $('#' + inlineRangeNavUrls[i][1])
        .hover(
          function(){showHideRangeSlidingTabs(curTab, true);},
          function(){showHideRangeSlidingTabs(curTab, false);} 
        );
      // establish per-loop variable by passsing as argument to anonymous function
      })(inlineRangeNavUrls[i][0]); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add scripting support for an Objective-C project using the objc runtime.
I want to recognize uppercase letters using objective-c. I have an implementation now that
The objective is simple, I want to create a method which load a class
I want to dynamically create variable from string in objective C. Like NSClassFromString thats
I want to write a program in Objective-c that grabs some sports stats off
I am using GNU GCC compiler(XCODE NOT AVAILABLE) for running objective C .I want
Objective: I want to send multiple images using multipart/form-data. Below is a snippet of
I want to have an array that is accessible throughout my Objective-C project whose
I want to convert html to pdf dynamically in objective c. what is the
Is it possible to create classes dynamically in Objective-C? I want to do something

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.