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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:54:31+00:00 2026-06-01T14:54:31+00:00

var message = ‘Spoon!’; $(‘#foo’).bind(‘click’, function() { alert(message); }); message = ‘Not in the

  • 0

var message = 'Spoon!';
$('#foo').bind('click', function() {
alert(message);
});

message = 'Not in the face!';
$('#bar').bind('click', function() {
alert(message);
});

Why are the two output messages are the same: “Not in the face!”;
Isn’t the first message within the closure of ‘foo’ refers to ‘Spoon!’?
Why not?
Please someone explain. I don’t understand the tutorial’s explanation.

  • 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-01T14:54:32+00:00Added an answer on June 1, 2026 at 2:54 pm

    That’s because the event handlers are launched asynchronously.
    While the message value you’re setting is done in the first thread.

    So basically your program will read your whole code, set the value to 'Spoon!', then to the last one you set 'Not in the face!'. Then when you click on either buttons it’ll alert the value of message 'Not in the face!'.

    Try putting message in the function, then you’ll see a different message for each. This would work as you expect as you set also the value asynchronously.

    $('#foo').bind('click', function() {
      var message = 'Spoon!';
      alert(message);
    });
    
    $('#bar').bind('click', function() {
      var message = 'Not in the face!';
      alert(message);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query: var req= (from tl in resultlist where (tl.Message.StartsWith(Do not
function helpLinkConvert(str, p1, offset, s) { return <a href=\look.php?word= +encodeURIComponent(p1)+\>+p1+</a>; } var message =
Here is my jQuery code: function onSaveClicked() { var message = { MessageID: $(#MessageID).val()
In the message object below, if I call it like this: var message_object =
I am sending to the server this call: var Message = {}; Message['islands'] =
This is my code to send mail (test code): //sending mail var message =
when I use MySQLdb get this message: /var/lib/python-support/python2.6/MySQLdb/__init__.py:34: DeprecationWarning: the sets module is deprecated
i have following variables in jquery var uemail=abc@domain.com,xyz@domain.com,gty@domain.com; var subj=test message; var text=<b>This is
I have a message box with 3 buttons: Yes, No, Help: var result =
I load a js variable like this: var message = '<%= CAnunturi.CPLATA_RAMBURS_INFO %>'; where

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.