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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:43:50+00:00 2026-06-14T11:43:50+00:00

I have a map of messages say: var Mapping = { notnow: 2, expensive:

  • 0

I have a map of messages
say:

var Mapping = {
"notnow": 2,
"expensive": 3,
"not_worth_it": 4
}

i have a bunch of html elements (lets say divs with the same name)
so

<div id="notnow"></div>

,etc

now i want to attach a click handler to each of them,
i run a loop as shown below

function setThemUp(){
   for(var item in Mapping)
   {
      $("#" + item).bind('click', function () {
      Apply(Mapping[item]); });
   }
}

But for some reason all of them seem to get bound to “not_worth_it”:4. Not to their respective values.

I’m using Jquery 1.5.

Can someone please explain why this might be happening?

My guess is that instead of the Mapping[item] being resolved to their values, it’s being passed as a reference or something, that’s why since the value of item eventually points to “not worth it” all of them call the function with that value itself. Any way in which i could overcome them.

Hard coding each of them as

  $("#notnow").bind('click', function () {
      Apply(Mapping["notnow"]); });
  $("#expensive").bind('click', function () {
      Apply(Mapping["expensive"]); });
  $("#not_worth_it").bind('click', function () {
      Apply(Mapping["not_worth_it"]); });

does work, but i would prefer an elegant solution with a loop.

Answer

i went with the closure solution

function setThemUp(){
   for(var item in Mapping)
   {
       $("#" + item).bind('click', (function () {
            return function(temp) {
                 Apply(Mapping[temp]); };
            })(item));
       }
   }

Reasons being , this was more of a why the loop didn’t work rather than about optimization of the jquery , since this was afterall a representative example, not my actual code, and this was an elegant solution to that problem.

  • 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-14T11:43:51+00:00Added an answer on June 14, 2026 at 11:43 am

    This is a classic case of scope issues: you’re referencing the variable item in each of your bound handlers. The variable item changes, though -> it’s being assigned all properties of the Mapping object literal, the last one being not_worth_it.

    Creating a closure might help, to preserve the state of item for each callback:

    for(var item in Mapping)
    {
       $("#" + item).bind('click', (function(currentItem)
       {//IIFE, pass item as argument---------/
            return function ()
            {//return function, \/ access to closure scope
                Apply(Mapping[currentItem]);
             };
        }(item)););
    }
    

    But this seems to be somewhat overkill, why not simply delegate the event, and use Mapping[$(this).attr('id')]?

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

Sidebar

Related Questions

I have a problem with Map objects, while using Gravity. All messages works ok,
I have map.resources :posts and I want to be able to serve post bodies
I have map with multiple different colors markers with some info window. Everything works
I want to display google map in Android ,i have map API even map
I have a Map and a List . The List should be sorted based
I have a map of the UK broken down into various geographic regions. I
I have a map that i want to update from a separate thread. Im
I have a map with various markers and i need to be able to
I have image map that can I move, but this map will be so
I have a map<int int> . I need to get a vector of the

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.