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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:34:08+00:00 2026-06-14T02:34:08+00:00

Possible Duplicate: Javascript closure inside loops – simple practical example I’m creating a table

  • 0

Possible Duplicate:
Javascript closure inside loops – simple practical example

I’m creating a table of statuses for a “dashboard” web page. Each status is in a state of “On” or “Off”.

I’m trying to use the jQuery button/buttonset to have switch/toggle controls to allow the user to toggle the value of a status. I create the table and buttons dynamically as the result of an ajax database query.

I’m basically trying to fire off an ajax query when the user clicks on one of the buttons, but I’m struggling to assign the click event properly.

My first go-round had the final list value fire no matter what button was clicked.

Therefore, Aha! It’s a standard “JavaScript closure” issue. (So I thought…)

Further playing around with it has yielded nothing, and I don’t really understand the closure issue well enough to figure out why my technique is wrong.

Here’s the code I want to call:

function SetStatus(item, status) {
    var params = '{itemName: "' + item + '", status: "' + status + '"}';
    $.ajax({
        type: "POST",
        url: "WebServices/Dashboard.asmx/SetItemStatus",
        data: params,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: StatusSet,
        error: ShowError
    });
}

Here’s the current version of code I’m trying:

$(itemOnButton).click(
    function (_item) {
        SetStatus(_item, 'On');
    } (itemName)
);

The result is that immediately upon getting the results from the database (before they’re even displayed), SetStatus fires for the last item with status == ‘On’.

I know this is simple, and I’m still convinced it’s a basic closure issue, but I’m banging my head against the wall! Thanks for any help!

  • 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-14T02:34:10+00:00Added an answer on June 14, 2026 at 2:34 am
    $(itemOnButton).click(
        function (_item) {
            SetStatus(_item, 'On');
        } (itemName)
    );
    

    Currently you are fireing your ‘closure’ function when assinging it to click. Try

    $(itemOnButton).click(
        function (ev) { 
            // this is the clicked element
            // ev is the event object
    
            // how do you get the name?
            // var itemName = $(this).attr("name");
    
            SetStatus(itemName, 'On');
        }
    );
    

    You could also do

    $(itemOnButton).click(
        function (_item) {
            return function (ev) {
                SetStatus(_item, 'On');
            };
        } (itemName)
    );
    

    Now it fires like you did, but it doesn’t execute immediate, instead it returns the event handling function for click.

    The returned event handling function is now the closure for _item. The anonymous function is the scope for _item, set to itemName by argument.

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

Sidebar

Related Questions

Possible Duplicate: Javascript closure inside loops - simple practical example Please explain the use
Possible Duplicate: Javascript closure inside loops - simple practical example I was trying to
Possible Duplicate: Javascript closure inside loops - simple practical example I add event handlers
Possible Duplicate: Javascript closure inside loops - simple practical example Calling setTimeout function within
Possible Duplicate: Javascript closure inside loops - simple practical example I am using webservice
Possible Duplicate: Javascript closure inside loops - simple practical example How can I make
Possible Duplicate: Javascript closure inside loops - simple practical example Rather than explaining the
Possible Duplicate: Javascript closure inside loops - simple practical example Javascript: closure of loop?
Possible Duplicate: Javascript closure inside loops - simple practical example Seen many posts talking
Possible Duplicates: Javascript closure inside loops - simple practical example Javascript closure “stores” value

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.