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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:46:02+00:00 2026-05-22T21:46:02+00:00

My code repeats as follows: $(#school-name).autocomplete(/ajax/campus_ajax.php, { width: 218, delay: 300, selectFirst: false, resultsClass:

  • 0

My code repeats as follows:

$("#school-name").autocomplete("/ajax/campus_ajax.php", {
    width: 218,
    delay: 300,
    selectFirst: false,
    resultsClass: 'ac_results_class',
    loadingClass: 'ac_loading',
    formatItem: function(data)
    {
        if (data[2]) 
        { 
            return data[0] + '<small>' + data[2] + '</small>'; 
        }
        else
        {
            return data[0];
        }
    }
});

$("#school-name-optional").autocomplete("/ajax/campus_ajax.php", {
    width: 358,
    delay: 300,
    selectFirst: false,
    resultsClass: 'ac_results_book',
    loadingClass: 'ac_loading',
    formatItem: function(data)
    {
        if (data[2]) 
        { 
            return data[0] + '<small>' + data[2] + '</small>'; 
        }
        else
        {
            return data[0];
        }
    }
});

As you can see, the two event listeners only differ in a couple of parameters. How should I shorten this code?

  • 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-22T21:46:03+00:00Added an answer on May 22, 2026 at 9:46 pm

    IMO, shortening this code makes it less readable and as configuration parameters outside the reach of DRY (Don’t Repeat Yourself).

    With that said, to answer your questions, there are two quick things you could do.

    First, break out the formatItem into a generic function instead of an anonymous function.

    function myFormatItem(data) {
    ...
    }
    

    then use:

    formatItem: myFormatItem
    

    Second, you could create the base configuration options, then extend them.

    var config = {width: 218,
            delay: 300,
            selectFirst: false,
            resultsClass: 'ac_results_book',
            loadingClass: 'ac_loading',
            formatItem: myFormatItem}
    
    $("#school-name").autocomplete("/ajax/campus_ajax.php", config);
    
    var config2 = config;
    config2.width = 358;
    $("#school-name").autocomplete("/ajax/campus_ajax.php", config2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

html in delete_define.php follows: <form action=delete_now.php onsubmit=my_funct();> <input type=submit name=my_submit class=my_submit value=submit/> </form> javascript
Any suggestions on how I can cleanup the following code pattern that repeats multiple
I've trying to create a stopwatch with HH:MM:SS, code is as follows: -(IBAction)startTimerButton; {
My code is as follows: protected override void OnStart(string[] args) { //Set the interval
I have a bash code as follows python $TM The problem is that $TM
I am implementing a timeout for the RKObjectManager. My code snippet is as follows:
i have used following code to repeat a process creation/close iteratively dim vProcessInfo as
Imagine that i have the following code: <a:repeat value=#{bean.getList()} var=x > <li class=la> <span>
I have such code in my JSF template: <h:form> <table id=users cellspacing=0> <a4j:repeat var=person
Could someone explain to me what this portion of code means? repeat scroll 0

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.