Activate a horizontal accordion when the button is clicked

I am looking to implement a dynamic horizontal accordion feature triggered by a button click.

I came across this example for creating a horizontal accordion. It works well, but it is limited to only 8 accordions. I want the flexibility for users to add as many accordions as they want.

I tried modifying the code to allow for dynamic creation of accordions when the button is clicked. The CSS code snippet below, taken from the provided link, limits the accordion count to 8. I need a solution to make it work for any number of accordions.

li:nth-child(1):nth-last-child(2) input[type="radio"]:checked ~ .accslide {
    width: calc(100% - 80px);
}

li:nth-child(8):nth-last-child(1) input[type="radio"]:checked ~ .accslide {
    width: calc(100% - 320px);
}

I am seeking assistance in modifying the CSS to accommodate a dynamic number of accordions. Any suggestions or guidance on this issue would be greatly appreciated.

Answer №1

I have updated it by including some JavaScript functionality. I hope this modification aids in your comprehension. This enhancement enables you to incorporate numerous tabs based on your requirements. I have endeavored to maintain simplicity and user-friendliness in the code. :)

let labels = document.querySelectorAll("label");

let totalWidth = 0;

labels.forEach(label => {
  totalWidth += label.clientWidth;
});

document.documentElement.style.setProperty("--label-width", totalWidth + "px");

View the updated code on Codepen: https://codepen.io/hunzaboy/pen/WNeOOPr

Answer №2

To create a design using exclusively CSS, you can take a look at this link: https://codepen.io/AsfanShaikh/pen/bGbRoXO. This example allows for dynamic accordion creation. However, keep in mind that the data is not contained within a specific container. If you wish to contain the data, you will need to incorporate some JavaScript to ensure proper functionality.

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

Conceal a column within a nested HTML table

I am facing a challenge with a nested table column structure: My goal is to hide specific columns based on their index within the table. Can someone explain the concept behind achieving this? I am unsure of how to get started on this task. <table clas ...

What is the reason for synchronous ajax calls in jQuery not having a timeout functionality implemented?

Whilst browsing this forum, I came across a discussion about how synchronous ajax calls in jQuery do not timeout. I'm curious to know if this is a technical limitation or simply a choice made by the developers. What really goes on behind the scenes w ...

Problem with styling a CSS table

I am having an issue with the table style. My goal is to set the table radius to 8px, but my code doesn't seem to be working. Can someone assist me with this? Thank you! //ps I also require border-collapse in my code. Here is the link to my jsfidd ...

Personalized design for the range input in React

I am working on a React component that includes an input range element. I have used some CSS to style it vertically and currently, it looks like this: https://i.stack.imgur.com/WmQP4.png My goal is to customize the appearance of the slider so that it res ...

Is the ASP AJAX control/extender failing to appear despite using the toolkitscriptmanager?

Struggling to incorporate a slider extender into one of the text boxes in my ASP web forms application using Visual Studio 2013. Despite multiple attempts and online tutorials recommending a switch from the original script manager to the AJAX toolkit scrip ...

Having trouble with CSS and javascript files not resolving after moving app to a new Windows 7 development machine

I have recently migrated my ASP.Net 3.5 web site from my old XP sp3 machine to a new Win 7 64-bit dev machine. The web application utilizes Master Pages, App_Themes with style sheets and images, as well as an image folder located off the main root. Additio ...

Looking to show an image when a checkbox is ticked and hide it when it is unticked in HTML?

How do I make a specific image appear when a user clicks on a checkbox? I'm unsure if I should use jQuery, Ajax, or another method. What is the best approach for this task? Your assistance would be greatly appreciated. I have successfully created the ...

Setting the file type when uploading content: a step-by-step guide

When uploading a file to an S3 bucket, it's essential to identify the file extension and add the correct content type. $('#upload-files').on('click', function(e) { e.preventDefault(); var fileName = data.files[0].name; var ...

Tips for implementing search suggestions onto an Ajax success event

I have been struggling to implement search suggestion in a text box. I've tried various methods but haven't found the right approach yet. I fetch data (arraylist) from the back end using ajax and return it to jsp as json. Now, I need to add the s ...

Can you guide me on how to activate the pickadate.js calendar above the input field?

Encountering an issue with the Pickadate calendar. There seems to be a problem at the bottom of the page, please refer to the attachment for more details. My challenge is that I need to display the Pickadate calendar above the input field when the field is ...

Troubleshooting issue: Asynchronous functionality not working with Ajax.BeginForm

Struggling to grasp ASP.Net MVC and facing challenges with using Ajax.BeginForm to update a partial view asynchronously. Here's the code snippet in the view for the action: @using (Ajax.BeginForm( new AjaxOptions { ...

The input value remains a string even after it has been converted to a float

I can't figure out where I'm going wrong. I'm attempting a simple task: getting user input, converting it to a float, and performing a calculation with that value. However, all I keep getting is NaN. Here's the input (I normally replac ...

The two divs are positioned on top of one another, with the link in the bottom div being unclickable

I am trying to create a unique effect where a tile divides into two on hover, with each tile acting as an individual link. Additionally, I want the background color of the tiles to change on hover. To achieve this, I have stacked two divs (toptile and bot ...

While HTML and CSS collaborate seamlessly during development, CSS mysteriously disappears when transitioning to production mode

I am brand new to this field and I apologize in advance for any mistakes. Currently, I am working on a node.js project using webpack and HTML loader. To test the project, I am utilizing the dev server dependency to run it locally. All models, views, and te ...

Is there a way to verify if a webpage was accessed using jQuery Mobile's back button?

Currently focusing on a jQuery project and would like to determine if the page was accessed through the user's "back" button within jQuery mobile (data-rel="back"), and not by using the browser back button. After conducting some research online and r ...

When the button is clicked, an input box and another button will be added to the appended fieldset

<script> $(document).ready(function () { $("#fieldsetnew-addphone").click(function () { var addphonenoinfieldsetnew = $(document.createElement('div')) .addClass('fieldset1-inner-phoneno'); addpho ...

What are the steps for submitting a form by clicking on text?

Whenever I try to submit a form by clicking on span id="ToolBarDownload", the form id="myformID" doesn't seem to get submitted. Can someone help me figure out what's wrong with my code? Any suggestions are greatly appreciated! Thanks! Also, just ...

Dealing with the complexities of Jquery Ajax and WCF Webservice integration:

I have developed a WCF Webservice method that accepts two parameters, both strings, and returns an XmlElement. Currently, I am encountering issues with an ASP.NET page that contains a JQuery AJAX call to this method. Despite trying to troubleshoot using o ...

When a user clicks, use Jquery to display the sidebar and hide it when

Hey, I could really use some help with this script. I've managed to get the panel to show with a mouse click, but I want it to close when the mouse leaves the panel. Here's a sample: http://jsfiddle.net/jikey/w9s7pt25/ $(function(){ $(' ...

Exploring Event Propagation in AngularJS

Currently, I am working on developing a web application using angularjs for the first time. A key feature that I aim to introduce is the ability for users to create a div in the main window upon clicking on an image in the menu. To achieve this functional ...