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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:00:24+00:00 2026-06-18T08:00:24+00:00

I experience a very weird JavaScript behavior. I’m using jQuery 1.9 and farbtastic color

  • 0

I experience a very weird JavaScript behavior.
I’m using jQuery 1.9 and farbtastic color picker:

While this code works:

$('#colorpicker_1').hide();
$('#colorpicker_1').farbtastic('#color_1');
$('#colorbody_1').click(function(){$('#colorpicker_1').slideToggle()});

$('#colorpicker_2').hide();
$('#colorpicker_2').farbtastic('#color_2');
$('#colorbody_2').click(function(){$('#colorpicker_2').slideToggle()});

$('#colorpicker_3').hide();
$('#colorpicker_3').farbtastic('#color_3');
$('#colorbody_3').click(function(){$('#colorpicker_3').slideToggle()});

$('#colorpicker_4').hide();
$('#colorpicker_4').farbtastic('#color_4');
$('#colorbody_4').click(function(){$('#colorpicker_4').slideToggle()});

this does not work, and i have absolutely no clue, why it does not:

var cstatusIDs = new Array();
cstatusIDs.push(1);
cstatusIDs.push(2);
cstatusIDs.push(3);
cstatusIDs.push(4);

for(var z=0; z < cstatusIDs.length; z++)
{
    var cstatus_id = cstatusIDs[z];

    console.log(cstatus_id); // outputs 1,2,3,4

    $('#colorpicker_'+cstatus_id).hide();
    $('#colorpicker_'+cstatus_id).farbtastic('#color_'+cstatus_id);
    $('#colorbody_'+cstatus_id).click(function(){$('#colorpicker_'+cstatus_id).slideToggle()});
}

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-06-18T08:00:26+00:00Added an answer on June 18, 2026 at 8:00 am

    The problem is the fact that cstatus_id is defined outside the scope of your click handler, and its value changes each time the loop runs. Since the click handler is not actually evaluated until you trigger a click event, at that time all of the handlers use the last value of cstatus_id.

    To work around this, wrap the handler in a closure so that each instance gets its own unique id.

    var cstatusIDs = [1, 2, 3, 4];
    
    for(var z=0; z < cstatusIDs.length; z++)
    {
        var cstatus_id = cstatusIDs[z];
    
        console.log(cstatus_id); // outputs 1,2,3,4
    
        $('#colorpicker_'+cstatus_id).hide();
        $('#colorpicker_'+cstatus_id).farbtastic('#color_'+cstatus_id);
    
        (function(id) {
            $('#colorbody_' + id).click( function() {
                $('#colorpicker_' + id).slideToggle()
            });
        })(cstatus_id)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apologies, as this may seem a very weird question. All of my experience in
I warn you now very little experience at jquery. My question is this. I
I have very little experience using Sharepoint but a good amount using Visual Studio
I have a very simple scenario but poor experience in this area and googling
A pleasant user experience requires a page to load very fast. This can be
I'm getting a very weird issue with standard ASP.NET backend code, and the latest
All right, this one is going to sound very weird and I don't know
I've got this to work using a very basic example(outlined below) and I want
Today i was debugging some lines of code and i noticed a very weird
I face a very weird experience, when i receive multiple notification and then didn't

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.