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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:15:20+00:00 2026-06-18T21:15:20+00:00

I really don´t know what is happening to me.. The code is like: for

  • 0

I really don´t know what is happening to me..

The code is like:

for (var j=0; j<tours.length; j++){
    var name = tours[j].name;

    var $tourLI = $('<li id="'+name+'"></li>');
    var $tourButton = $('<div class="button-inside"><span>'+name+'</span><span></span></div>');

    $tourButton.click(function() {
        alert(name);
    }
}

I´m trying to bind the click event for each button which displays the tours name, but is always displaying the last tour name whichever the button I’m clicking.

What am I doing wrong?

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-06-18T21:15:21+00:00Added an answer on June 18, 2026 at 9:15 pm

    You need to wrap the click handler in a closure to ‘close-over’ the value of the variable name before the for-loop moves on.

    This is because the handler isn’t actually executed until you click on it, so otherwise it will simply use the current value of name at that time (whatever the last value in the loop was).

    for (var j=0; j<tours.length; j++){
        var name = tours[j].name;
    
        var $tourLI = $('<li id="'+name+'"></li>');
        var $tourButton = $('<div class="button-inside"><span>'+name+'</span><span></span></div>');
    
        (function(n) {
            $tourButton.click(function() {
                alert(n);
            });
        })(name)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a strange behavior in my code and I really don't know how
I don't know why this is happening. I have this code here . It
I really don't know much about Apache, I followed some tutorials and installed the
I really don't know squat about java memory management, so this is entirely my
I really don't know how to explain what's going on, so I'll just show
I'm new to python so I really don't know the language very well. the
I'm stuck on a problem I really don't know how to solve: I have
Ok i decided to post the question here because i really don't know what
I'm relatively new to Objective-C and really don't know much about it yet, so
Currently I'm trying to implement something in my app where I really don't know

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.