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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:03:44+00:00 2026-05-13T12:03:44+00:00

I have a bunch of elements with ids like check1, check2, check3 etc. I

  • 0

I have a bunch of elements with ids like “check1”, “check2”, “check3” etc. I want to loop through each of them and hide them and set onclick funcion. Here is my current code:

for (i = 0; i < badgeArray.length; i++) {
        jQuery("a#check"+ i + "").click(function(){
            jQuery("#check" + i + "info").toggle();
            jQuery("a#check"+ i + "").toggleClass("extrasopen");
            jQuery("a#check"+ i + "").toggleClass("extrasclosed");
        });
        jQuery("#check" + i + "info").hide();
    }   

All “#check” + i + “info” elements are hidden, but hide/display functionality doesn’t work. When I try it just for one element like this:

jQuery("a#check1").click(function(){
    jQuery("#check1info").toggle();
    jQuery("a#check1").toggleClass("extrasopen");
    jQuery("a#check1").toggleClass("extrasclosed");
}); 

It works.

Any ideas?

  • 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-13T12:03:44+00:00Added an answer on May 13, 2026 at 12:03 pm

    You need to use a closure:

    for (var i = 0; i < badgeArray.length; i++) {
        (function(i) {
            jQuery("a#check"+ i).click(function(){
                jQuery("#check" + i + "info").toggle();
                jQuery("a#check"+ i).toggleClass("extrasopen").toggleClass("extrasclosed");
            });
            jQuery("#check" + i + "info").hide();
        })(i);
    }
    

    But you could also use the contextual element and its properties:

    for (var i = 0; i < badgeArray.length; i++) {
        jQuery("a#check"+ i).click(function(){
            jQuery("#" + this.id + "info").toggle();
            jQuery(this).toggleClass("extrasopen").toggleClass("extrasclosed");
        });
        jQuery("#check" + i + "info").hide();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 2 select dropdowns each of them have bunch of elements (just
I have a bunch of < li > elements which I want to animate
I have a bunch of elements like the following: <div class=droppableP id=s-NSW style=width:78px; height:63px;
I have a bunch of elements like this: <div></div> <span></span> <table></table> <div></div> <span></span> <div></div>
I have a bunch of inline-block elements over several lines which I'd like to
I have a bunch of elements that look like this: <span class='tags' id='html'>html</span> <span
I have a bunch of li elements that I want to alternate in color
I have a bunch of elements that look like <div id=username-2343></div> I need to
I have a bunch of elements that look like.. <div id=hi> <div class=head> </div>
I have a bunch of a elements and I want to add a GET

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.