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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:25:08+00:00 2026-05-27T02:25:08+00:00

I have a problem with my code bellow: for (var i =0; i <

  • 0

I have a problem with my code bellow:

for (var i =0; i < filterArray.length; i++)
{

    name1 = filterArray[i];
    selectstring = 'a[cat="'+name1+'"]';
    filterMenuItem.addItem(name1 , function() {$(selectstring).show(); alert(selectstring)});


}

filterArray contains 2 items currently.. “Foo” & “Bar”..

now this “addItem(name1” works fine. We get an added menu item with the name Foo and another named Bar

the issue comes with the next part (the function).

both the Foo & Bar menu items functions end up doing the same thing. (e.g. they both get the select string ‘a[cat=”bar”]’… as tested with the alert)

Now i assume this is happening because the selectstring variable is being passed by reference to the function? So when i’m setting selectstring value a second time it is overriding the value from the first loop?

How can I pass a unique copy of selectstring to a selector? i tried “function(selectstring) {….}” but this didn’t help..

Thanks for anyone who can shed some light on the subject!

EDIT: Revised code below.. Still same issue:

for (var i =0; i < filterArray.length; i++)
{

    var name1 = filterArray[i];
    var selectstring = 'a[cat="'+name1+'"]';

    filterMenuItem.addItem(name1 , function() {alert(selectstring); $('.sortablelist').hide();  $(selectstring).show();});


}
  • 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-27T02:25:09+00:00Added an answer on May 27, 2026 at 2:25 am

    It has nothing to do with “passing by reference” to the function — nothing is passed to the function, actually.

    I think the problem is the variable scope. You didn’t use var before selectstring on the assignment line, so it’s assumed to be a global variable. The anonymous function will keep a reference to this global variable, which will have the value of ‘bar’ by the time the function is run. If you use var selectstring = (...), a separate closure will be created on each iteration, and it should work.

    EDIT

    I answered too fast, and the answer was wrong, so here is an updated solution. I assumed a closure was being created when the anonymous function was declared, but that’s not the case. So the actual solution involves creating a closure to retain the variable’s value for each loop iteration:

    for (var i=0; i < filterArray.length; i++) {
        addFilteredMenuItem(filterArray[i]);
    }
    
    function addFilteredMenuItem(itemName) {
        var selectstring = 'a[cat="'+itemName+'"]';
        filterMenuItem.addItem(itemName, function() {
           alert(selectstring);
        });
    }
    

    Working example on jsfiddle

    The solution I posted as a comment below also works, and is based on the same principle, but I think it looks bad for involving an unnecessary self-executing function.

    See also this SO thread discussing the same subject.

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

Sidebar

Related Questions

I have some problem with my code public IQueryable<PageItems> GetPageById(Guid Id) { var xml
I have some problem with my cfml website. I have used the below code
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem with this code: file = tempfile.TemporaryFile(mode='wrb') file.write(base64.b64decode(data)) file.flush() os.fsync(file) # file.seek(0)
Hi i have problem with this code, i found it on the internet and
i am a beginner and i have a problem : this code doesnt compile
Possible Duplicate: What's wrong with Delphi's “with” I am have a problem debugging code
i have a problem with my code. foreach (DataRow dr in dt_pattern.Rows) { part
I have a problem in php code inserting values into database (I use PHPMyAdmin).
I have a problem with the code analysis rule CA1726:UsePreferredTerms . Our business domain

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.