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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:39:40+00:00 2026-05-25T12:39:40+00:00

This is in JQuery, using the JQuery-UI version of autocomplete I’m in a bit

  • 0

This is in JQuery, using the JQuery-UI version of autocomplete

I’m in a bit of an edge case. I’m working inside a form that must be indefinitely cloneable, so I can’t use id tags for unique identification, and I can’t just search down from the top to find something that looks like the DOM elements I’m working with – I have to use relative functions (.closest, .find, and so forth). Inside this form, I have a setup with a country field, a state field, and a location field, and the location field is supposed to be an autocomplete, fed from a link, where it hands the current value of the country and state fields on the link going up for server-side filtering. I’d like it to go something like this:

function locPrep(locNode) {
  $locNode.autocomplete({
    source: function (request, response) {
      $locDiv = $(this).closest("div.locWrapper");
      $stateNode = $locDiv.find("input.state");
      $countryNode = $locDiv.find("input.country");
      $.post("/FormHost/AutocompleteLocList",
        {state:$stateNode.val(), country:$countryNode.val(), term:request.term },
        function (data) {
          response(data);
        }
      );
    }
  });
}

…except that that doesn’t work because the $(this) for the source function isn’t locNode, and I can’t just declare it outside and use it inside because locPrep gets called repeatedly, once for each autofill in the form. If I try that, the fact that Jquery makes everything global causes all of the autofills in the form to reference the country and state fields for the last one. I tried using javascript vars to get around the scoping issues and it broke strangely. Can anyone help?


Edit: thanks to Dmitriy Naumov’s kind assistance, I was able to hammer together a functioning solution (though I’m still not entirely sure why it works, given some of the things that I tried that did not). I’m including the functioning code in the hopes that it may help someone else later. (I’ve certainly gained quite a lot from the answered questions of others.)

First, I went through and changed every instance where I called the function from “locPrep(foo);” to “new locPrep(foo);” I also changed it to take locDiv rather than locNode. Then, I changed the code to the following (though this is, as that was, a somewhat simplified version).

function locPrep(locDiv) {
  $locNode = $(locDiv).find("input.locationAutofill")
  $locNode.autocomplete({
    source: function (request, response) {
      var stateVar = $(locDiv).find("input.state").val();
      var countryVar = $(locDiv).find("input.country").val();
      $.post("/FormHost/AutocompleteLocList",
        {state:stateVar, country:countryVar, term:request.term },
        function (data) {
          response(data);
        }
      );
    }
  });
}

I’m not entirely sure which bits of this were necessary, but as a group they manhandle the code around to using tight enough scoping to solve the issue.

  • 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-25T12:39:41+00:00Added an answer on May 25, 2026 at 12:39 pm

    Save $(this) to a variable and pass it as a parameter (locNode) to your function (during initialization). Also, instead of $(this) you can pass a link to the container element, so each of your initialized autocompletes will know its parent.

    Update: The following may not be 100% correct in terms of autocomplete initialization, but its a solid illustration how to create 2 unique instances of locPrep in <div id="myDiv1"> and <div id="myDiv2">

    myContainer1 = $('#myDiv1');
    myContainer2 = $('#myDiv2');
    
    myAutocompleteInstance1 = new locPrep(myContainer1);
    myAutocompleteInstance2 = new locPrep(myContainer2);
    

    Update2: how to pass this

    function locPrep(locNode, currentThis) {
    
     /// use currentThis instead of $(this) inside locPrep function
    
      $locNode.autocomplete({
        source: function (request, response) {
          $locDiv = currentThis.closest("div.locWrapper");
          $stateNode = $locDiv.find("input.state");
          $countryNode = $locDiv.find("input.country");
          $.post("/FormHost/AutocompleteLocList",
            {state:$stateNode.val(), country:$countryNode.val(), term:request.term },
            function (data) {
              response(data);
            }
          );
        }
      });
    }
    

    init:

    myAutocompleteInstance1 = new locPrep(myContainer1, $(this));
    

    new will make sure that value of currentThis is unique in each instance of locPrep

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

Sidebar

Related Questions

I'm using JQuery and Prototype in this web application that I'm developing. I'm looking
I have this version of jQuery-autocomplete: jQuery Autocomplete Mod , It's cool with me
I've been using the JQuery Autocomplete plugin with JQuery version 1.3.2, and it has
Why doesnt this work? $('#websitedesign').load('test.html', function() { alert('Load was performed.'); }); Jquery version using:
This is my second attempt at a php contact form using Jquery and php
I'm using the jQuery UI Autocomplete plugin (version 1.8), and I'd like to customize
Can anyone explain this strange behaviour in jQuery? I'm using version 1.5.2. var myxml
I've found this hack to use jTemplates with the jQuery UI Autocomplete: http://www.shawnmclean.com/blog/2011/02/using-jqueryui-autocomplete-with-jtemplates/ but,
I am using jQuery (version 1.6.2) to generate an animation that works well on
I need to change an element's ID using jQuery. Apparently these don't work: jQuery(this).prev(li).attr(id)=newid

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.