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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:26:43+00:00 2026-06-11T15:26:43+00:00

I am new to front end programming. When I was learning jQuery I came

  • 0

I am new to front end programming. When I was learning jQuery I came across this issue. Here is my code:

<script type="text/javascript">
$(document).ready(function(){
  var msg ="hi";
  $("#test1").click(function(){alert(msg)});
  msg ="hello";
  $("#test2").click(function(){alert(msg)});
});
</script>
</head>
<body>
  
<button id="test1">test1</button>
<button id="test2">test2</button>

On running this code, both the alerts prints "hello". I except the first button on clicking should alert with "hi". Why does it behave like this? Is this the right behavior? Is it because of scope constraint?

  • 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-11T15:26:44+00:00Added an answer on June 11, 2026 at 3:26 pm

    Lots of mostly correct explanations here but, much to my surprise, no one’s thrown out the key concept here: closure.

    Basically what’s happening is when you declare your two functions, the scope they are declared in forms a closure. That means that the variables inside that closure remain available to the functions. In other words:

    $(document).ready(function(){
        // this begins a closure
        var msg ="hi";
        // you are simply declaring a function here, not calling it
        $("#test1").click(function(){alert(msg)});
        msg ="hello";
        // ditto
        $("#test2").click(function(){alert(msg)});
        // the end of the closure...msg has the value "hello"
    });
    

    Then some time passes, and the click events get called. The functions that are attached to the click events still have access to the closure (inside which the value of msg is “hello”).

    The traditional way to “capture” a variable’s value inside a closure is to create an “Immediately Invoked Function Expression” (IIFE). Basically you can think of this as creating an entirely new closure that contains the immediate value of a variable. You could re-write your code to use IIFEs like this:

    $(document).ready(function(){
        // this begins a closure
        var msg ="hi";
        // the following creates an anonymous function with a single parameter
        // AND invokes it immediately, creating another closure in which the
        // value of msg1 is "hi".
        (function(msg1){
            $("#test1").click(function(){alert(msg1)});
        })(msg);
        msg ="hello";
        // ditto
        (function(msg2){
            $("#test2").click(function(){alert(msg2)});
        })(msg);
    });
    

    I hope this makes it a little bit clearer what’s happening, and how to get what you’re looking for.

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

Sidebar

Related Questions

I am new to javascript and the whole front-end side of development. Here is
This is my front-end code (using fetch ) var MyModel = Backbone.Model.extend(); var MyCollection
I'm somewhat new to Wordpress. I'm primarily a front-end web designer, and I use
How does Udacity.com (new learning website) create presentation where the text is in front
I'm basically a front end designer and new to PHP and MySQL but I
As a new comer to writing front-end views with Rails, I'm a bit confused
I have the following front end code: <ajaxToolkit:TabContainer runat=server Height=150px Width=70% ActiveTabIndex=1 OnDemand=true AutoPostBack=false
I'm new and don't have much experience in database. I've a front end GUI
I'm new to backbone and trying to write an html5 front end for an
I created a new Control TestControl . On the front-end I gave it <asp:Label

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.