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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:27:46+00:00 2026-05-20T10:27:46+00:00

I would like each click is associated with the right occurrence in the table

  • 0

I would like each click is associated with the right occurrence in the table boxes.

var J = jQuery.noConflict();

const   bNumber = 2;
var boxes   = new Array(bNumber);

boxes[0]    = new Array("#cch", "#cc");
boxes[1]    = new Array("#sinh", "#sin");

for(var k=0;k<bNumber;k++) {
 J( boxes[k][0] ).click( function() {           
  //J( boxes[k][1] ).toggle();
 });
}

With this solution, each click is associated with boxes[2][1]

  • 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-20T10:27:46+00:00Added an answer on May 20, 2026 at 10:27 am

    JavaScript has no block scope, only function scope. That is why creating functions in a loop is tricky. Every closure has a reference to the same variable (k in your case). After the last iteration of the loop, k has the value 2.

    To capture the current value of the loop counter you have to introduce a new scope, i.e. call a function. E.g. with an immediate function:

    for(var k=0;k<bNumber;k++) {
        (function(index) {
            J( boxes[index][0] ).click( function() {           
                J( boxes[index][1] ).toggle();
            });
        }(k));
    }
    

    Another way is to create a function that generates the click handler:

    function getClickHandler(element) {
        return function() {element.toggle()});
    }
    
    for(var k=0;k<bNumber;k++) {
        J(boxes[k][0] ).click(getClickHandler(J(boxes[k][1])));
    }
    

    OT:

    You should not use new Array to initialize arrays. There is no need to define the size of the array beforehand. Using array literal notation is more concise:

    var boxes = [["#cch", "#cc"], ["#sinh", "#sin"]];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this little click counter. I would like to include each click in
I am using jquery and I am looping like: $(span).each(function (index) { var idname
I would like to run multiple websites, and I would like each website (differentiated
I would like to replace each two spaces from the beginning of each line,
I have a div that I would like to reposition each time the page
I've a collection of a class' properties and would like to update each one's
I would like to get numbers of comments for each posts using mysql. I
I would like to implement a distinct thread for each route in apache camel.I
I would like to check the memory consumption of each running processes individually, cat
I would like to add track code for each link of in my email

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.