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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:19:48+00:00 2026-05-13T21:19:48+00:00

I have this JavaScript code: for (var idx in data) { var row =

  • 0

I have this JavaScript code:

for (var idx in data) {
    var row = $("<tr></tr>");
    row.click(function() { alert(idx); });
    table.append(row);
}

So I’m looking through an array, dynamically creating rows (the part where I create the cells is omitted as it’s not important). Important is that I create a new function which encloses the idx variable.

However, idx is only a reference, so at the end of the loop, all rows have the same function and all alert the same value.

One way I solve this at the moment is by doing this:

function GetRowClickFunction(idx){
    return function() { alert(idx); }
}

and in the calling code I call

row.click(GetRowClickFunction(idx));

This works, but is somewhat ugly. I wonder if there is a better way to just copy the current value of idx inside the loop?

While the problem itself is not jQuery specific (it’s related to JavaScript closures/scope), I use jQuery and hence a jQuery-only solution is okay if it works.

  • 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-13T21:19:49+00:00Added an answer on May 13, 2026 at 9:19 pm

    You could put the function in your loop:

    for (var idx in data) {
      (function(idx) {
        var row = $("<tr></tr>");
        row.click(function() { alert(idx); });
        table.append(row);
      })(idx);
    }
    

    Now, the real advice I’d like to give you is to stop doing your event binding like that and to start using the jQuery “live” or “delegate” APIs. That way you can set up a single handler for all the rows in the table. Give each row the “idx” value as an “id” or a “class” element or something so that you can pull it out in the handler. (Or I guess you could stash it in the “data” expando.)

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

Sidebar

Ask A Question

Stats

  • Questions 509k
  • Answers 509k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Like this: private static readonly Dictionary<BookCode, Func<Book>> ctorsByCode = new… May 16, 2026 at 4:44 pm
  • Editorial Team
    Editorial Team added an answer They are definitely guidelines, but if you don't have confidence… May 16, 2026 at 4:44 pm
  • Editorial Team
    Editorial Team added an answer I don't think there is one in the framework itself.… May 16, 2026 at 4:44 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

i have this js code : var str = javascript:__doPostBack('ctl00$M$List$_rli2$ctl06',''); alert (str); var str
I have this following code $(function () { $.post(fetch_data.php?url=+$('#url').val(), { }, function(response){ var arrValues
I have this javascript code below that uses jquery, it is suppoed to be
Hi Javascript gurus, I have this Javascript code which is working fine on Firefox
I have this <ul> <ul id=select_opts class=bullet-list style=margin-left:15px;></ul> This javascript code which is meant
I have got this JavaScript code for uploading files to my server (named it
Below i have pasted my piece of javascript code here 1) function showCustomerName(dropdown) {
I have the javascript code below which disables an asp.net button during page postback.
I have some javascript code that is used in a few different pages in
I have a web page with embedded PDF on it. My code looks like

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.