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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:01:26+00:00 2026-05-26T04:01:26+00:00

Ive got a problem with the following loop: for (var i = 0; i

  • 0

Ive got a problem with the following loop:

for (var i = 0; i < dataElements; ++i){
  d=document.createElement('div');
  $(d).addClass('overviewbit')
  .appendTo('.overview')
  .click(function(){
    id = i;
  });
}

Every Div sets id to the highest value of the loop but i should be the exact value i gets when its created. So the first div should set it to 1, the second div should set it to 2 and so on. i hope you understand my problem and could help me to find a solution.

  • 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-26T04:01:26+00:00Added an answer on May 26, 2026 at 4:01 am

    This a common problem. When you create the click handler, it’s setting id to i — the variable, not the value i was storing at the time.

    The for loop is completing before any of the DIVs are clicked, so i is equal to the final value from the loop for all the click handlers, and as a result all the ids are set to the same value.

    With jQuery you can solve this problem by using .data() storage:

    for (var i=0; i<dataElements; ++i){
        d=document.createElement('div');
        $(d).addClass('overviewbit')
            .appendTo('.overview')
            .data('val',i)
            .click(function(){
                    id = $(this).data('val'); 
                });
    }
    

    However, to do things the “proper” way you would use a JavaScript closure:

    for (var i=0; i<dataElements; ++i){
        d=document.createElement('div');
        $(d).addClass('overviewbit')
            .appendTo('.overview')
            .data('val',i)
            .click((function(j){
                    return function() { id = j; }
                })(i));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ive got a following problem. I made a XUL toolbar. Toolbar works correctly, it
I've got following problem: (c#) There is some class (IRC bot), which has method,
I've got the following problem: I have two tables: (simplified) +--------+ +-----------+ | User
I've got a problem with the following code pasted below, the problem seems to
Hi guys, ive got a stupid problem. My Custom handler is working 100% on
I've got the following problem and I can't seem to get why it's not
I've got the following problem: I need to write a decorator that would be
I've got the following problem: I created a WebPart with a ToolPart, this toolpart
Ive got this code: loadData : function(jsonArray) { var id = $(this).attr(id); for(var i
I've got the following problem. After starting, application works fine - even after changing

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.