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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:37:50+00:00 2026-05-29T23:37:50+00:00

I looked at the post jQuery: Loop iterating through numbered selectors? and it didn’t

  • 0

I looked at the post jQuery: Loop iterating through numbered selectors? and it didn’t solve my problem, and didn’t look like it was truly an answer that works.

I have a list of <h3> tags that are titles to questions, and there are answers below in a <p>. I created classes for each Q & A like so:

<h3 class="sec1">Question:</h3><p class="view1">Answer...</p>
<h3 class="sec2">Question:</h3><p class="view2">Answer...</p>
<h3 class="sec3">Question:</h3><p class="view3">Answer...</p>

I used the following jQuery loop to reduce redundacy for my 21 questions.

$(document).ready(function () {
    for (var i = 1; i < 21; i++) {
        var link = ".sec" + i;
        var content = ".view" + i;

        $(link).click(function () {
            $(content).toggle("fast");
        });
    }
});

But it isn’t working for all Q & A sets, only the last one. i.e.: It works for the first set if I set the max value to 2 (only looping once). Please advise. Thanks

  • 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-29T23:37:51+00:00Added an answer on May 29, 2026 at 11:37 pm

    While I agree with @gaffleck that you should change your approach, I think it is worth while to explain how to fix the current approach.

    The problem is that the click function does not get a copy of the content variable but instead has a reference to that same variable. At the end of the loop, the value is .view20. When any element is clicked it read that variable and gets back .view20.

    The easiest way to solve this is to move the code into a separate function. The content variable within this function is a new variable for every call of the function.

    function doIt(i){
        var link = ".sec" + i;
        var content = ".view" + i;
    
        $(link).click(function () {
            alert(content);
        });
    }
    
    $(document).ready(function () {
        for (var i = 1; i < 21; i++) {
            doIt(i);    
        }
    });
    

    http://jsfiddle.net/TcaUg/2/

    Notice in the fiddle, if you click on a question the alert has the proper number. Optionally, you could make the function inline, though I find the separate function in most cases to be a bit cleaner.

    http://jsfiddle.net/TcaUg/1/

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

Sidebar

Related Questions

I looked at previous post based on this but they do not relate. I
I looked through some code and noticed that the convention was to turn pointer
I looked through the other posts and bug reports and couldn't figure out what's
I'm having a problem when using the jquery .each() and .ajax() functions together. I'm
Using the submit function from jquery, how can I get it to post to
I recently had a little problem using a jquery plugin with coffeescript (within the
I have looked all over the web for a solution to this problem. But
I've looked through all the stuff to remove characters from strings but I don't
I have looked at several tutorials involving rails 3 and jquery. The tutorial at
I have used the jQuery template system and have looked at the data link

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.