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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:25:07+00:00 2026-06-12T01:25:07+00:00

Got a problem here and i’ve been pulling my hair last few days… I’m

  • 0

Got a problem here and i’ve been pulling my hair last few days…

I’m making a sort of language filter function for a website and it’s a menu where you click a language and it filters out the rest of them. Since i got lots of different languages i figured it would be best to loop everything instead of making like 15 pieces of the same code.

Here’s my code:

// >>> Language click effects
    for(langClickNum = 1; langClickNum < langList.length; langClickNum++) {

        $('#lang'+langList[langClickNum]).click(function() {
            if (!langSelect[langClickNum]) {

                clrSearch();
                clrLang();
                langHide();

                $('#lang'+langList[langClickNum]).addClass('langCheck');
                $('.itemLang'+langList[langClickNum]).show();
                langSelect[langClickNum] = true;
            }
            else {
                clrLang();
                langShow();
            }
        });

    }

As you can see inside the click function I want to pick indexes from different arrays using the loop counters number. But since the script inside the click function dosent run unless I click it, it won’t catch the correct array number. Instead it picks the last array number for each language i click. I’m very aware of what problem is but I have no idea how to solve it. Please help me out there!

If I give it numbers not looping it it works just fine, like this:

$('#lang'+langList[1]).click(function() {
        if (!langSelect[1]) {

            clrSearch();
            clrLang();
            langHide();

            $('#lang'+langList[1]).addClass('langCheck');
            $('.itemLang'+langList[1]).show();
            langSelect[1] = true;
        }
        else {
            clrLang();
            langShow();
        }
});

I hope you can see my 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-12T01:25:09+00:00Added an answer on June 12, 2026 at 1:25 am

    The problem is related to the concept of JS variable scoping/closure.

    The value of langClickNum iterates ok when you are binding the ‘click’ handler, but when the click handler is executed, the ‘langClickNum’ is already equal to langList.length.

    you could do something like:

    for(x = 1; x < langList.length; x++) {      
        (function(){
            var langClickNum = x;   
            $('#lang'+langList[langClickNum]).click(function() {//... rest of your code}
        })();
    }
    

    Here, the value of langClickNum will be as you expected, because of the function scoping in JS.

    see for more information:
    https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope

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

Sidebar

Related Questions

I've got a weird problem here. I've been trying to find out whats wrong
Hello fellow programmers, got a few problem here. I am adding a user control
got a few problem here and hoping that you could help me out. Let
Got a few problem here. I have created a arrays of EditText and it
I want to ask about strcpy. I got problem here. Here is my code:
I've got an interesting box-model problem here. I have a header full of links,
I've got a quite strange problem here. I'm calling some simple code via Ajax.Updater:
Can't understand what is a problem here: I have got main.cpp file where I
Here i got a problem in my script, Once we set one radio button
Got a problem which to me make no sense. So here goes: I have

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.