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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:34:10+00:00 2026-05-22T22:34:10+00:00

Is there a way to set the width of an autocomplete object dynamically, based

  • 0

Is there a way to set the width of an autocomplete object dynamically, based on the length of text returned to it?

Thanks

  • 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-22T22:34:11+00:00Added an answer on May 22, 2026 at 10:34 pm

    This one took a bit of effort but I believe I have a reasonable solution. Primarily this requires binding to the “open” event of the autocomplete widget in order to change the default behavior of the menu as it displays, as well as modifying a few CSS attributes to help us out.

    The method comes with one caveat: this will cause the background bars to only span the length of the text rather than all the way across the menu in IE7. It will function normally in IE8+ as well as all other major browsers. I would discourage you from treating it as an issue, as you’d be tailoring your solution to a visual disparity for less than 3% of the market and probably even less of your actual viewership.

    The CSS

    .ui-autocomplete {
        max-height: 200px;
        overflow-y: auto;
        overflow-x: hidden;
    }
    
    .ui-autocomplete .ui-menu-item a {
        padding-right: 20px;
    }
    

    Add these styles to your project to help with visual display. The .ui-autocomplete styles are fairly standard to create a scrollable list of menu items and you are likely already using it. The right padding applied to the anchor is to allow for the proper amount of room for a scrollbar in Firefox and Webkit browsers. It works better than applying it directly to the UL container.

    The jQuery

    $(".myAutoComplete").autocomplete({
        ...
        open: function(e, ui) {
            $("ul.ui-autocomplete:visible").css("width", "").find("a").html(function(i, html) {
                return html.replace(/\s/g, " ");
            });
        }
        ...
    });
    

    The handler bound to the open event is mostly obvious in what it does. First, the selector it uses looks for the only autocomplete menu visible. Thankfully, the open event is raised only after the autocomplete menu has been shown. This will help your handler direct its attention in the event you have more than one autocomplete widget on a page. From there, it removes the “width” attribute that’s applied directly to the CSS of the unordered list. Then, it finds all anchor descendants in the list and does a global replace on all white space in its HTML with non-breaking spaces. The nbsp forces the anchor to respond by increasing its width rather than wrapping text to a new line, thereby increasing the width of the entire menu.

    You can see a working demo here. I’d suggest typing in “ab” followed by “deep” to see the differences in resizing as they happen. Sorry for the long list of colors (sorce) in the code, but I wanted to give you the opportunity to get a good range of possible suggestions to view differences in the resizing.

    EDIT

    Don’t know what I was thinking back then, but a much easier approach would be to provide the following additional style and ditch the open event handler:

    .ui-autocomplete .ui-menu-item a {
        white-space: nowrap;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way in CSS to set the width of any element to
Is there any way how to set std::setw manipulator (or its function width )
Is there a way to set the length of all TD cells in case
Is there any way to set a fixed/custom height for ligtbox2? #lightbox img{ width:
I'm trying to set the width of a Textfield() object based on it's string
Is there a way to set the width of all td's in the same
Is there any way to set the height/width of a LayoutParams as density-independent pixels
Is there any way to set the column width of the result of desc
I was wondering if there's a way to set a ComboBox' width as wide
I'm wondering if there is a way to set a ColumnDefinition Width to *

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.