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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:52:52+00:00 2026-06-01T21:52:52+00:00

I have the following code: for (_field in _fields) { $.post( ‘/api/fields’, { id:

  • 0

I have the following code:

for (_field in _fields) {
    $.post(
        '/api/fields',
        { id: _field.id },
        function(data, _field) {
            alert(data);
        } (data, _fields[_field)
    );
}

I have to pass the _fields[_field] element to the function that returns the data from the jQuery because loses the reference to the right object during the loop. The problem is that in defining that the post function should have a _field parameter, you also have to specify a parameter for data, or data will be overwritten with _field.

Currently data returns as undefined because I have no data object defined inside the loop. I also tried passing in null, but that also just returns null. I’m looking for a way to pass the element without overwriting the data returned from the post function.

Is there any way to fix this, or is there perhaps an alternative jQuery method that can do what’s needed?

  • 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-01T21:52:54+00:00Added an answer on June 1, 2026 at 9:52 pm

    You want a function factory function — a function that creates a function:

    for (_fieldName in _fields) {
        $.post('/api/fields',
        {
            // Unrelated, but I think this bit is wrong; shouldn't it be
            // `id: _fields[_fieldName].id` ? You're trying to use `.id` on
            // a string -- see below for a full update
            id: _fieldName.id
        },
        makeHandler(_fields[_fieldName])
        );
    }
    function makeHandler(field) {
        return function(data) {
            // Use `data` and `field` here
        };
    }
    

    Note that in the object initializer we’re passing into $.post, we’re calling makeHandler to it runs and returns the function we’ll then pass into $.post. That function is then called when the $.post completes, and has access to the data argument that $.post gives it as well as the field argument to makeHandler, because it’s a closure over the context of the call to makeHandler, which includes the field argument. More: Closures are not complicated


    Note that in the code above, I changed your variable _field to _fieldName to be more clear: The variable in for..in loops is a string, the name of a property. See also the comment, I think you were trying to use .id in the wrong place. Here’s what I think you really wanted:

    for (_fieldName in _fields) {
        _field = _fields[_fieldName];
        $.post('/api/fields',
        {
            id: _field.id
        },
        makeHandler(_field)
        );
    }
    function makeHandler(field) {
        return function(data) {
            // Use `data` and `field` here
        };
    }
    

    Also note that if _fields is an array, you shouldn’t use for..in on it without safeguards. More: Myths and realities of for..in

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

Sidebar

Related Questions

I have the following code in the OnChange() event for a field. alert(alert text);
I have the following code where the function codeaddress geocodes the text feild value
I have the following code. I want to add two input fields together and
I have the following code: <form action= method=POST> <?php $count = isset($_POST['count']) ? $_POST['count']
i have the following code and the call back function is not being called
On a report I have the following code for a field: =Sum([PartQty]*[ModuleQty]) Example results
I have the following code which displays and enables a hidden field if 'Other'
I have the following code, which should adds a field to a form if
I have the following code for my custom Constraint: @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD
I have the following code: <? $query =$db->prepare(INSERT INTO a_table (id, a_field) VALUES ('',

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.