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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:23:51+00:00 2026-05-25T13:23:51+00:00

I have two jQuery UI auto-completes on the same page and I’d like to

  • 0

I have two jQuery UI auto-completes on the same page and I’d like to make the code more “functional” and terse. My background is almost strictly OO and I’d like to get more serious about writing more functional JavaScript.

Both of these functions are properties of an object literal that I’m using to namespace the functions on the page. Right now there is a lot of code that’s repeated between the functions and I’d like to use something similar to the “partial application” pattern to reduce the code.

autocomplete 1:

initProject : function(){
    var selected = 0;
    var suggestions = []; 
    var projs;
    var len;

    $("#projectNum").autocomplete({
        source : function(req, add){
            $.getJSON("projectList.do?method=viewProjectListJSON&contractID=" + req.term, function(data){
                //clear the suggestions array
                suggestions = [];

                projs = data[0];

                len = projs.length;

                for(var i = 0; i < len; i++){
                    suggestions.push(projs[i][1]);
                };
                add(suggestions);   

            });//end getjson callback
        }, 

        minLength : 2,

        select : function(){
            thisval = $(this).val();

            for(var i = 0;i < len; i++){

                if(thisval === projs[i][1]){
                    $("#projectID").val(projs[i][0]);
                    return;
                }   
            }
        }
    })
},

autocomplete 2:

initCAU : function(){
    var selected = 0;
    var suggestions = [];
    var caus;
    var len;
    $("#cauNum").autocomplete({
        source : function(req, add){
            $.getJSON("cauList.do?method=viewCAUListJSON&cauNumber=" + req.term, function(data){
                suggestions = [];

                caus = data[0];
                len = caus.length;

                for(var i = 0; i < len; i++){
                    suggestions.push(caus[i][1].toString());
                };
                add(suggestions);   
            }); //end getjson callback
        }, 

        minLength : 2,

        select : function(){
            thisval = $(this).val();

            for(var i = 0;i < len; i++){

                if(parseInt(thisval,10) === caus[i][1]){
                    $("#cauID").val(caus[i][0]);
                    return;
                }   
            } 
        }
    })
},
  • 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-25T13:23:51+00:00Added an answer on May 25, 2026 at 1:23 pm
    //factored-out common code...
    var autocompleter = function(urlPrefix, fragment) {
      var selected = 0;
      var suggestions = [];
      var items;
      var len;
      return ({
        minLength: 2,
        source: function(req, add) {
          $.getJSON(urlPrefix + req.term, function(data) {
              suggestions = [];
              items = data[0];
              len = items.length;
              for(var i = 0; i < len; i++) {
                suggestions.push(projs[i][1]);
              };
              add(suggestions);
            }); //end JSON callback
        }, //end source callback
        select: function() {
          var thisVal = $(this).val();
          for(var i = 0; i < len; i++) {
            if(thisVal === items[i][1]) {
              $(fragment).val(items[i][0]);
              return;
            }
          }
        } //end select callback
      });
    };
    
    //verbose but clear way to achieve what you were doing before.
    var initCAU = function() {
      var attachTo = "#cauNum";
      var urlPrefix = "cauList.do?method=viewCAUListJSON&cauNumber=";
      var fragment = "#cauID";
      $(attachTo).autocomplete(autocompleter(urlPrefix, fragment));
    };
    
    var initProject = function() {
      var attachTo = "#projectNum";
      var urlPrefix = "projectList.do?method=viewProjectListJSON&contractID=";
      var fragment = "#projectID";
      $(attachTo).autocomplete(autocompleter(urlPrefix, fragment));
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two scripts running on the same page, one is the jQuery.hSlides.js script
So I have two jquery functions that bassically do the same, but on 2
I have two select lists, I would like jquery to either remove or disable
I'd like to check ancestry using two jQuery objects. They don't have IDs, and
I have two jQuery autocomplete textboxes on a mvc web page. One that returns
I have built two control containers on a page, one is using jquery-ui tabs
I have two pages of jQuery, Page1 and Page2, and I'm able to get
Assuming I have the following two JQuery functions - The first, which works: $(#myLink_931).click(function
How do I have two effects in jQuery run in sequence , not simultaneously?
I'm using the jQuery datepicker where I have two textboxes (start date / end

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.