The jQuery "Chosen" select accordion is experiencing the issue of its height becoming too large and then snapping back into place

I have integrated jQuery Chosen with a Twitter Bootstrap Accordion. The issue of the Chosen dropdown being clipped by the accordion body has been resolved by referring to solutions provided in this thread. However, I am still encountering another problem where the accordion expands too much in height and then snaps back to the correct height.

To better illustrate the issue, you can view this in action on this jsfiddle link.

Code for Reproducing the Problem

The following code snippet is used to reproduce the height issue within the accordion:

<div class="accordion" id="accordion1">
    <div class="accordion-group">
        <div class="accordion-heading"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion1" href="#body1">First group</a>
        </div>
        <div class="accordion-body collapse" id="body1">
            <div class="box-content">
                <span>Body content!</span>
                <select multiple="multiple" class="chzn-select">
                    <option>Option A</option><option>Option B</option><option>Option C</option><option>Option D</option><option>Option E</option><option>Option F</option><option>Option G</option>
                </select>
            </div>
        </div>
    </div>
</div>

Chosen dropdown activation is performed using the script below:

$(document).ready(function() {
    $('.chzn-select').chosen();
});

The CSS used to mitigate the overflow issue and aid debugging is as follows:

/* Styles for visualizing the problem: */
body, .box-content { padding: 10px; }
.accordion-body { background-color: #EEE; }
.accordion-heading { background-color: #DDD; }

/* Required to make sure the chosen select can expand outside the box: */
.collapse.in { overflow: visible; }

Attempted Solutions

I have tried various approaches to address this issue:

  • Exploring different methods to fix the overflow problem. However, it seems that my issue persists regardless of the overflow settings.
  • Utilizing a standard select works as a temporary workaround, but not a permanent solution like seen here.
  • Applying height: 0 and display: none on the Chosen select had no impact.

Query

How can I prevent the accordion from expanding excessively in height initially and transition smoothly to the correct size right away?

Answer №1

If you're looking to achieve this effect with a touch of JavaScript, take a look at this JSFiddle. Here is the essential code snippet:

$(document).ready(function() {
    $('.chzn-select').chosen();

    $(".box-content") .hover(function(){
        $(".chzn-container .chzn-drop") .css({"display":"block"});
    },function(){
        $(".chzn-container .chzn-drop") .css({"display":"none"});
    });    
});

Alternatively, you can address the issue using CSS with these rules:

.accordion-body .chzn-container .chzn-drop { display: none; }
.accordion-body[style="height: auto;"] .chzn-container .chzn-drop { display: block; }

Take a look at the demo in this JSFiddle link.

Answer №2

Here's a straightforward CSS fix:

.chosen-container will receive the class chosen-with-drop when the dropdown is activated.

.chosen-container:not(.chosen-with-drop) .chosen-drop {
    display: none;
}

I encountered the same issue today and this solution has been successfully tested.

Answer №3

Following along the lines of Chris' suggestion; when utilizing bootstrap accordions, you have the option to simply conceal the list as the collapse event occurs in order to prevent the calculation of its height.

.collapse, .collapsing {
  &:not(.in) .dropdown-menu {
    display: none;
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a button that utilizes jQuery to send data is a straightforward process

After utilizing Google and this platform, I've managed to create a basic form for sending data using jQuery. Here's my HTML code: <a href="javascript:AddPoke('{news-id}', 'poke')">ADD</a> <div class="area"> ...

Can you share a method in javascript that extracts href= and title values, similar to PHP's preg_match_all() function?

I received an HTML string as :var code; I am looking to retrieve all the values of href and title similar to how it is done using PHP's preg_match_all(). I have achieved a similar task in PHP with the provided example but now I am curious about how I ...

Methods for transferring data to controller in Angular Modal service

After implementing the angular modal service discussed in this article: app.service('modalService', ['$modal', function ($modal) { var modalDefaults = { backdrop: true, keyboard: true, m ...

"Triggering the jQuery mouseout event following a resize of an element

I'm currently trying to develop a dynamic shopping cart widget. The concept is to have a box that displays the number of items in your cart, and when you click on it, it expands to show a detailed view of the cart contents. I've successfully man ...

Editing the dimensions of the input in Bootstrap 4

Is there a way to adjust the width of the input and select elements? I want the input element to take up more space while the select element occupies less space. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel=" ...

web font causing div not to resize to accurate height

Having some webfont service issues with jscrollpane. It seems like the height of jscrollpane is being calculated based on the default font size before switching to the webfont, causing the div to be slightly longer and cutting off text. I tried using auto ...

I would like a div element to slide up from the bottom of the page

Could someone please assist me in creating a popup div that appears from bottom to top when a specific button is clicked? I would like the div to be fixed without affecting the overall page content. Any help would be greatly appreciated. Thank you! $( ...

The -moz-use-text-color property does not function properly in Chrome and IE versions 9 and above

I have added -moz-use-text-color for border-color in the following CSS code: .booksline{ border-image: none; margin: 0 auto 0px; width: 96%; height:220px; padding:20px 20px 20px 0; border-width: 1px 1px medium; border-style: solid solid none; border-colo ...

to streamline the process of navigating Google Chrome web pages

Is it possible to create automation in Google Chrome that can complete the following tasks: 1. Input a job name to monitor 2. Periodically click a refresh button on the webpage (not the refresh button for the entire Chrome page in the left corner) 3. Open ...

The personalized confirmation dialog is experiencing malfunctions

I have designed my own custom dialogs using Bootstrap and jQuery, such as Alert and ConfirmDialog. Here is a sample: http://jsfiddle.net/eb71eaya/ The issue I am facing is that in the callback function, I make an AJAX call. If it returns true, I want to ...

IE11 alternative for the CSS property "unset"

I have a div fixed to a specific location on my webpage using the following CSS properties: width: 320px; height: 160px; position: fixed; right: 15px; bottom: 15px; top: unset; z-index: -1; While the div displays correctly in other browsers, in Internet ...

Tips on customizing the appearance of JavaScript output?

I recently created a plugin for my website with JavaScript, and one of the lines of code I used was output.innerHTML = "Test"; Is it possible to apply CSS styles to this element, or is there an alternative method? ...

Issue with Laravel 5.4: AJAX behaving unexpectedly and not returning errors as it should

After going through several tutorials on handling AJAX requests in Laravel, I'm still facing some issues. Each tutorial has its own approach... Finally, one method is not giving me a 500 error, but it's not displaying validation errors as expect ...

Issues are arising with Bootstrap's functionality within the Vite React app

I am new to working with react and vite. I am currently developing a vite react application that requires the use of bootstrap. I followed the instructions provided on the official Bootstrap website here. However, not all Bootstrap functionalities are work ...

Finding the class name of the parent div: A simple guide

Within a maze of nested divs, there lies a checkbox waiting to be deciphered. When the OnChange event triggers, I aim to unveil the hidden class name of the parent div/container div known as pan-box placeholder. The HTML journey begins with this structure ...

When the modal is closed, the textarea data remains intact, while the model is cleared

My challenge involves a simple modal that includes a text area. The issue I am facing is resetting the data of the textarea. Below is the modal code: <div class="modal fade" ng-controller="MyCtrl"> <div class="modal-dialog"> <d ...

Navigate a user upon completion of an update

My webpage requires users to click an update link, which triggers a process of fetching data from an API that takes a few minutes. I don't want users to have to constantly refresh the page to know when the process is complete and they can continue. Is ...

There seems to be a problem with the object as

While working with signalR hub, the variable "name" functions smoothly in all other methods except for the following one where it returns as "undefined." In this particular method, the "name" is supposed to be added to a list and eventually displayed in a ...

Is there a way to showcase the outcome of a Python script on an HTML webpage?

Hey there, I'm a beginner to coding and I've been working on a little project to track the price of gold in a web application using Flask and Python. In my HTML code, I have a button that, when clicked, takes users to a new route where the gold ...

Can jQuery Ajax() be configured to mimic the accept-charset parameter in an HTML form?

I am sending SVG data to a processing page using "accept-charset='utf-8'". The data is then inserted into a table on the processing page. Later, I retrieve the data from the table on another page and use Batik to convert it into a PNG file succes ...