Build a dynamic <div> using jQuery inside another collapsible <div>

While working on creating an expandable/collapsible Div-Container, I came across this code that has been a lifesaver for me as a novice designer.

Now, I'm looking to add an expandable div within one of these containers. The challenge I'm facing is how to modify the function so that it automatically adjusts the outer height of the initial expandable div. I have provided an example below:

Collapsible 1

  • Collapsible 1-1
  • Collapsible 1-2
  • Collapsible 1-3

You can find the code I'm currently using here:

HTML:

<button id="button">Toggle Expand/Collapse</button>
<div id="wrapper" class="open">
  <ul id="list">
    <li>Item</li>
    <li>Item</li>
    <li>Item</li>
    <li>Item</li>
  </ul>
</div>

CSS:

#wrapper {
background: #ccc;
overflow: hidden;
transition: height 200ms;
}    

JS:

$(function() {
var b = $("#button");
var w = $("#wrapper");
var l = $("#list");

w.height(l.outerHeight(true));

b.click(function() {

 if(w.hasClass('open')) {
   w.removeClass('open');
   w.height(0);
 } else {
  w.addClass('open');
  w.height(l.outerHeight(true));
 }

 });
 });

Answer №1

If you want to avoid calculating height, consider using the .slideDown and .slideUp methods.

$(document).ready(function() {
  $('#button').click(function() {
    $('#wrapper').toggleClass('open').slideToggle(200);
  });
});
#wrapper {
  background: #ccc;
  overflow: hidden;
  /*transition: height 200ms;*/
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="button">Toggle Expand/Collapse</button>
<div id="wrapper" class="open">
  <ul id="list">
    <li>Item</li>
    <li>Item</li>
    <li>Item</li>
    <li>Item</li>
  </ul>
</div>

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

Can text automatically adjust its size based on the browser's dimensions using CSS?

Can text automatically resize as the browser size decreases for liquid-based design using percentages? While images and divs can rescale, text scaling in percentages seems impossible! When set in percentages, it only changes the unified em setting for tha ...

Customizing Your WordPress Menu with HTML5 Features

Can someone assist me in creating a customized WordPress menu? I am facing issues with unnecessary classes added by WordPress and the lack of certain attributes on dropdowns. Below is the HTML code: <nav class="navbar navbar-default navbar-static- ...

Running a JQuery function within an Angular expression

Can Angular expressions execute JQuery code? Here's an example: <span>{{$("[for='loanType']").text()}}</span> I attempted it, but haven't been successful so far. Is there a way to make it work without creating a key/value ...

Continuously update in JavaScript based on a condition, cease updating when the condition no longer

I have a scenario on my page where specific data needs to be refreshed every minute, but at the same time, the user should be able to view and edit certain modals. I want to ensure that when a modal is open, the data refreshes are paused so that the modal ...

Encountering an unexpected token error while attempting to incorporate JQuery into a React JS project

I am currently diving into the world of React.js and attempting to incorporate a side navigation bar on my homepage. However, I encountered an eslint error when trying to implement the sidebar using jQuery code. Below you will find the code snippet for the ...

Achieving Full Screen Height in Angular Component

Trying to nest an angular component within another one that occupies the entire screen using height:100%; in the component's css file. I want to stack them and ensure each component takes up the full screen height, but setting height:100% doesn't ...

Monitor File Tool for JetBrains IDE - CSS Optimization Setup - Streamline CSS Automatically

Utilizing the File Watcher tool in Jetbrains IDE (Webstorm and Rider) to automatically minify .css files and generate corresponding .min.css files for the entire project has been my goal. However, a challenge arises when it starts minifying files that alr ...

The caret plugin feature in jQuery is dysfunctional in Internet Explorer

After successfully creating a complex script using the jQuery plugin at caret (http://code.google.com/p/jquery-at-caret/), I unfortunately discovered that it only functions properly in Firefox and Chrome, but not in Internet Explorer. The issue seems to b ...

Leverage JavaScript to retrieve content and generate an iframe

Can you use JavaScript to extract and insert <div> content from another website into the current site? For example: If Website 1 has a portal and wants to include content from Website 2. Website 2 contains the required content within the <div i ...

Implementing jQuery function to hide or show content upon page load

Is there a way to have the content hidden when the page is first opened, but then revealed once a button is clicked? Any suggestions? <script> $(document).ready(function(){ $("#rooms").click(function(){ $("#rooms_box").show(); }); $("#faciliti ...

Convert an entire HTML page into a PDF file by generating it with JSPdf

I've included all necessary js files on my aspx page to generate a pdf file of my table data. However, the generated pdf comes out blank. I want to ensure that all table data is included in the pdf. Does anyone have a solution for this? <script src ...

Update the Div when Fancybox is Closed

Is there a way to refresh a parent page's div after closing Fancybox? I am aware of using onClosed: function(), but I'm unsure which specific function to include in it. ...

FancyBox: Struggling to adjust dimensions accurately

Currently using Fancybox 1.2.6 in conjunction with JQuery 1.2.6, Sample Code: <script type="text/javascript> $(document).ready(function() { $("a.iframe").fancybox({ 'width' : 300, 'hei ...

Modifying Background Images Dynamically with jQuery as You Scroll

Struggling to make my background image change while scrolling has been a challenge for me. I've tried different solutions suggested for similar questions, but so far no success - it only displays the initial background image. The setup involves havin ...

Ways to alter the color of the <div> Element based on its height or width?

My goal is to dynamically change the color of a div element based on its height. Here's the criteria: If the div's height is less than or equal to 20%, I want it to be colored green. If it's above 20%, the color should be blue. I'm lo ...

Display the current index carousel caption from Bootstrap 4 in a separate div

I'm attempting to showcase the text from the carousel in Bootstrap 4 within another container. Currently, I can retrieve the current index and the text individually, but I am struggling to combine them together. Below is my code, would appreciate an ...

Why does having a floating div and a div positioned below an absolutely positioned div result in additional margin?

When trying to create a two-column layout with a left floating navigation div and an absolute positioned header, I encountered an issue where the navigation div had an extra margin of 4em. This seemed unnecessary to me as it disrupted the expected layout f ...

managing the speed at which animated gifs are loaded and played

I am facing a challenge with a slideshow that features multiple animated gifs. Each slide is equipped with its own animated gif. My goal is to ensure that the animated gifs play in synchronization with the timeline I have set up in photoshop, starting fro ...

Tips for achieving seamless scrolling with the combination of javascript and css

Looking for a solution to achieve smooth scrolling on an HTML page without using the #id_name to scroll to a specific div. Instead, I want to scroll to a particular point, such as having a button that scrolls down the webpage smoothly by 250px when press ...

Verifying the Page Load Status in the Browser

I've been spending a lot of time lately trying to find code that will allow me to trigger an action after the browser finishes loading a page. After much searching, I finally came across this piece of code on <script> var everythingLoaded = s ...