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

  • Home
  • SEARCH
  • 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'm developing a web service (in asp.net) and I would like to have each
I would like to know how to loop through each line in a text
I would like to execute a stored procedure over each row in a set
I would like the version property of my application to be incremented for each
I have two strings and I would like to mix the characters from each
I have a DataGrid where each column has a SortExpression. I would like the
Given a list of urls, I would like to check that each url: Returns
I have a table setup like this (simplified for example): user_id item_id click_dt Each
I'm new to working with video. I would like to put some tiny video
Would like to get a list of advantages and disadvantages of using Stored Procedures.

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.