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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:24:35+00:00 2026-06-17T10:24:35+00:00

Possible Duplicate: How do JavaScript closures work? I’ve read all the million duplicates of

  • 0

Possible Duplicate:
How do JavaScript closures work?

I’ve read all the million duplicates of the same old javascript closure loop issue. I thought I understood them, and have been using closures for months without issue, until today. I am stumped.

for (var i in groups){
    for(var j in groups[i]){
        $(unique_form).die('submit').live('submit'), function{
             function (groups2, i2, j2){
                 return function(){alert(groups2[i2][j2])}
              }(groups, i, j)
             }
         });
        }               
    }
 }

When I submit each unique form – I keep getting an alert for the last element of groups[i][j]. Obviously, I’m doing something stupid, what is it? I thought that by creating the anonymous function groups2, i2, and j2 I was solving the problem.

  • 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-17T10:24:37+00:00Added an answer on June 17, 2026 at 10:24 am

    The function you pass to .live() is executed when the submit event is fired. By that point, i and j will have their final values. You would need to create the closure outside of the event handler:

    (function (i2, j2) {
        $(unique_form).die('submit').live('submit', function{
            alert(groups[i2][j2])
        });
    }(i, j));
    

    Notice that I’ve removed the groups argument from the anonymous function. There’s no need to close over that since its value won’t change. I’ve also wrapped the entire function expression in parentheses, which is the convention (and in this case actually required, to force it to be parsed as an expression).

    Also note that I’ve removed the closing parentheses that you have after the first argument to .live() in your question. It shouldn’t be there.


    Side note.

    .live() and .die() have been deprecated for ages now. Use .on() (with jQuery 1.7+) or .delegate() (with older versions).

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

Sidebar

Related Questions

Possible Duplicate: Event handlers inside a Javascript loop - need a closure? I am
Possible Duplicate: How do JavaScript closures work? Javascript closures and side effects in plain
Possible Duplicate: Event handlers inside a Javascript loop - need a closure? I have
Possible Duplicate: Javascript: closure of loop? I have following code inside javascript: for (var
Possible Duplicate: JavaScript closures and variable scope Assign click handlers in for loop I
Possible Duplicate: Javascript infamous Loop problem? I am having a small issue, and it
Possible Duplicate: How do JavaScript closures work? I was playing around with the Google
Possible Duplicate: Event handlers inside a Javascript loop - need a closure? I want
Possible Duplicate: Event handlers inside a Javascript loop - need a closure? I am
Possible Duplicate: Event handlers inside a Javascript loop - need a closure? for i

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.