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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:11:21+00:00 2026-06-12T17:11:21+00:00

Possible Duplicate: Assign click handlers in for loop I need help with a loop

  • 0

Possible Duplicate:
Assign click handlers in for loop

I need help with a loop in my code.

I loop through an array and add on clicks to divs. But it always adds onclicks to the last cycle through the loop and effectively cancels out the ones before it.

So i have this as my loop:

    start = 0;

for(i = start; i < (start+8); i++){ //8 per page
    if(i == array.length){ 
        break;  //page end
    } else {
        (function(i){
             document.getElementById('cell'+i).onclick = function(){ select(i); }
        })(i);  
    }
}

What occurs here is div id cell7 gets the on click added, but the div ids cell0 to cell6 do not. I’m guessing its something to do with the fact that i changes in the loop and so is also effected the i in the function ?

How do I fix this 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-12T17:11:22+00:00Added an answer on June 12, 2026 at 5:11 pm

    I’m not sure why but these suggestions didn’t fix my problem – but i managed to find a way around it by adding to the onclicks as i display the divs during the loop so i ended up doing:

    innerHTML = '<Div onclick="">'; //etc etc
    

    Instead of adding the divs to the output “then” assigning the onclicks later… don’t know if my new method is the best choice but it works so i’ll have to stick to it!

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

Sidebar

Related Questions

Possible Duplicate: JavaScript closures and variable scope Assign click handlers in for loop I
Possible Duplicate: Assign click handlers in for loop I'm a bit naive in javascript.
Possible Duplicate: Adding css class through aspx code behind Im trying to assign a
Possible Duplicate: Event handlers inside a Javascript loop - need a closure? I want
Possible Duplicate: Assign a method for didEndOnExit event of UITextField How can I add
Possible Duplicate: Possible to assign to multiple variables from an array? If in python
Possible Duplicate: PHP reindex array? Is there a specific PHP function to assign new
Possible Duplicate: Algorithm for generating a random number Hi i need to assign a
Possible Duplicate: How do i Loop through the hidden field items and put in
Possible Duplicate: Create shortcut to console.log() In javascript we can easy assign functions to

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.