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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:26:00+00:00 2026-05-15T18:26:00+00:00

I have a code like this where I am trying to add an event

  • 0

I have a code like this where I am trying to add an event handler to some button. I would like to use a global variable & store its current value in callback closure & not its reference.

var globalNum="dummy";
function A()
{
  $("#button").click(function()
  {
     $("#button").append(globalNum);
  });
}

globalNum="dummyAgain";

Now if click event is fired what would be added – “dummy” or “dummyAgain” ?
I believe it would be “dummyAgain” coz for closure global variable’s reference is stored. I want value to bind.
I know I can create a local variable inside A which I can initialize with global variable & bind that but is there some other cooler way around too?

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-15T18:26:00+00:00Added an answer on May 15, 2026 at 6:26 pm

    You are right, it would append dummyAgain. You can deal with this by using bind() and send some event data:

    var globalNum="dummy";
    (function A()
    {
      $("#button").bind('click', {localNum: globalNum}, function(e)
      {
         $("#button").append(e.data.localNum);
      });
    })();
    
    globalNum="dummyAgain";
    

    Note that I immediately execute the function A, so the click handler gets attached before the value of globalNum changes. As @Andrew points out in his comment, this is the same effect as not using the “wrapping” function A at all.


    Same solution without bind:

    (function()
     {
      var localNum = globalNum;
      $("#button").click(function()
      {
         $("#button").append(localNum);
      });
    })();
    

    Here the anonymous function is useful to capture the the current value globalNum in a local variable.


    Update:

    For the sake of completeness (I hope, @Andrew, you don’t mind that I put it here). The “coolest” way is probably to use a parameter for the function and execute the function immediately with this parameter:

    var globalNum="dummy";
    
    (function(globalNum)
     {
      $("#button").click(function()
      {
         $("#button").append(globalNum);
      });
    })(globalNum);
    
    globalNum="dummyAgain";
    

    The “trick” is that the name of the parameter and the name of the global variable are the same.

    This is also very often used to refer to some object with a shorter identifier without polluting the global namespace, e.g. with jQuery:

    (function($) {
        // jQuery == $
    })(jQuery)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code like this in a winforms app I was writing to
I have some code like this: If key.Equals(search, StringComparison.OrdinalIgnoreCase) Then DoSomething() End If I
I have code like this: var newMsg = new Msg { Var1 = var1,
I have code like this: template <typename T, typename U> struct MyStruct { T
I have code like this to move the player in my game left, right,
We currently have code like this: Dim xDoc = XDocument.Load(myXMLFilePath) The only way we
Suppose I have code like this: template<class T, T initial_t> class Bar { //
If I have code like this: public XALServiceConfiguration CreateInstance() { var config = ConfigurationManager.GetSection(ConfigurationSectionName)
I need to move backwards through an array, so I have code like this:
I'm using PHP5 to create XML files. I have code like this: $doc =

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.