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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:26:38+00:00 2026-05-12T19:26:38+00:00

Anyone that can help me out will be very much appreciated. I seem to

  • 0

Anyone that can help me out will be very much appreciated. I seem to have got myself into a bit of a kerfuffle.

I’m trying to assign response data – retrieved from an AJAX request using the jquery.autocomplete plugin – to an input element. I can get the response data back fine, but when I try and insert it into the ‘value’ attribute, nothing happens!

$('#btnPopulate').click(function() {
if ($('#list_length').val() != '') {
    var length = $('#list_length').val();
    var row='';
    for (var i=0; i <= length; i++) {
        row="<tr><td>"+i+"</td><td><input name='data[CompanyRanking]["+i+
         "][company_id]' id='CompanyRanking"+i+
         "CompanyId' value='1'></td><input type='hidden' name='data[CompanyRanking]["+i+
         "][ranking]' id='CompanyRanking"+i+"Ranking' value='"+i+"'></tr>";
        $('#ranking_table').append(row);
        $("CompanyRanking"+i+"CompanyId").autocomplete({ 
            serviceUrl:'/backend/companies/search',
            minChars:2, 
            delimiter: null, // regex or character
            maxHeight:400,
            width:300,
            deferRequestBy: 50, //miliseconds
            // callback function:
            onSelect: function(value, data){ 
                alert('You selected: ' + value + ', ' + data); 
                                $('#CompanyRanking'+i+'CompanyId').val(data);
                alert('val: '+
                             $('#CompanyRanking'+i+
                             'CompanyId').val() + 'object?: '+
                             $('#CompanyRanking'+i+'CompanyId'));
               }
        });
            };
    //$('#btnPopulate').ac;
    } else {
    alert('You must first specify how many companies are in the list.');
};
    return false;
});
  • 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-12T19:26:39+00:00Added an answer on May 12, 2026 at 7:26 pm

    It would seem to be a problem of scope. Try creating a closure around the value of i inside the loop:

    onSelect: (function(i) {
        return function(value, data){ 
            $('#CompanyRanking'+i+'CompanyId').val(data);
        }
    }(i))
    

    A better explanation can be found in this answer:

    The problem you have here is that the
    variable item changes with each loop.
    When you are referencing item at some
    later point, the last value it held is
    used. You can use a technique called a
    closure (essentially a function that
    returns a function) to quickly scope
    the variable differently.

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

Sidebar

Related Questions

Can anyone help? I have some code that is shared between 2 projects. The
can anyone help. I have recently moved servers, its IIS7 rather that our previous
Can anyone help me with sample code for a compass needle that points in
Can anyone help me with creating a regex for variables in java so that
Can anyone help me with error_log file. If you already guessed that I am
Can anyone help me to do this in the right way?I mean.. like that
Can anyone recommend tools or articles that help me to learn how to TDD
A plea for help! Can anyone share a simple GWT2.0 project that uses the
I'm having a problem that I can't seem to figure out, though I'm not
I'm getting very frustrated and I'm hoping someone here can help. I have a

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.