Adjusting the vertical alignment of wrapped divs with jQuery toggle

I am currently working on a project where I have a set of dynamically generated divs, each containing rows of content with three columns. Using jQuery toggle, I am trying to show and hide certain elements within the divs without affecting their layout. Here is an example of what I have:

<script type="text/javascript">
    $(".trigger").click(function () {
        var trigger = $(this);
        $(this).next(".toggle").slideToggle();

    });
</script>
<div id="toggleContainer">
    <div>
        <h3 class="trigger">Trigger 1</h3> 
        <ul class="toggle">
            <li>Line One</li>
            <li>Line Two</li>
            <li>Line Three</li>
        </ul>
    </div>
    ...
</div>

You can see the functionality in action in this JSFiddle. However, my challenge now is how to move the toggled content down vertically while keeping the rest of the rows intact. What I want to achieve is something like this:

A1 A2 A3

B1 B2 B3

C1 C2 C3

If we click on A1 and toggle its content, the new state should look like:

A1 A2 A3

A1toggle B2 B3

B1 C2 C3

C1

Answer №1

To achieve overlapping of content onto the next row, you simply utilize the position:absolute property for the toggle class and adjust the z-index accordingly.

Answer №2

When organizing your content, try arranging it vertically instead of horizontally. This way, you can have a structure similar to the one shown below:

$(".trigger").click(function() {
  var trigger = $(this);
  $(this).next(".toggle").slideToggle();

});
div#toggleContainer {
  width: 100%;
  display: flex;
  justify-content: space-evenly;
}

div.content {
  flex: 0 0 33%;
}

h3 {
  font-size: 2em;
  position: relative;
  cursor:pointer;
}

.toggle {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="toggleContainer">
  <div class="content">
    <h3 class="trigger">Trigger 1</h3>
    <ul class="toggle">
      <li>Line One</li>
      <li>Line Two</li>
      <li>Line Three</li>
    </ul>
    <h3 class="trigger">Trigger 4</h3>

    <ul class="toggle">
      <li>Line One</li>
      <li>Line Two</li>
      <li>Line Three</li>
    </ul>
    <h3 class="trigger">Trigger 7</h3>

    <ul class="toggle">
      <li>Line One</li>
      <li>Line Two</li>
      <li>Line Three</li>
    </ul>
  </div>
  <div class="content">
    <h3 class="trigger">Trigger 2</h3>

    <ul class="toggle">
      <li>Line One</li>
      <li>Line Two</li>
      <li>Line Three</li>
    </ul>
    <h3 class="trigger">Trigger 5</h3>

    <ul class="toggle">
      <li>Line One</li>
      <li>Line Two</li>
      <li>Line Three</li>
    </ul>
    <h3 class="trigger">Trigger 8</h3>

    <ul class="toggle">
      <li>Line One</li>
      <li>Line Two</li>
      <li>Line Three</li>
    </ul>
  </div>
  <div class="content">
    <h3 class="trigger">Trigger 3</h3>

    <ul class="toggle">
      <li>Line One</li>
      <li>Line Two</li>
      <li>Line Three</li>
    </ul>
    <h3 class="trigger">Trigger 6</h3>

    <ul class="toggle">
      <li>Line One</li>
      <li>Line Two</li>
      <li>Line Three</li>
    </ul>
    <h3 class="trigger">Trigger 9</h3>

    <ul class="toggle">
      <li>Line One</li>
      <li>Line Two</li>
      <li>Line Three</li>
    </ul>
  </div>
</div>

I've also utilized the flex property for better alignment of the elements.

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

Utilizing JQuery and AJAX to dynamically populate a select menu with XML data

I am facing an issue with populating a drop-down menu from an XML file using a specific script. Let me share the script I have been working with: $(document).ready(function(){ // loading jQuery 1.5 function loadfail(){ alert("Error: Failed to r ...

What is causing these headers for the children not to center properly?

I am trying to centrally align both vertically and horizontally the <div> element, with its child elements <h1> and <h2> being horizontally centered in relation to the parent <div>. To achieve this, I have created a specific class f ...

Store data in Laravel's storage directory

I encountered an issue while trying to save files to the storage folder in Laravel after deploying the project on a web server (byethost7.com). The files were only being stored in the public folder. I ran the following command in the command prompt: >p ...

Tips for moving the title to the next line within a card design

I'm currently working on an antd Card and I am trying to figure out how to display the title on two lines if it is too long. For example, instead of showing "This title needs to go to ne...", I would like it to be split into two lines as "This title n ...

How can CSS be instructed to "apply the following most specific rule in order to determine this property"?

Utilizing Material-UI and JSS for CSS management, I've encountered an issue where styles appear differently in development versus production. The discrepancy stems from the order of rules within the file. For instance, when defining an element like ...

Which SSO framework works best for integrating Facebook and Twitter logins?

I own a website and I'm looking for a way to let users leave reviews and rate products. I need an SSO system that allows them to sign in with their Facebook or Twitter accounts and ensures each user is uniquely identified so they can't rate produ ...

How can I retrieve user input from a text box and showcase it in an array format using jQuery?

I successfully wrote a JavaScript code where I take input values from a text box, add them to an array using an 'add' button, and display the array values when the 'display' button is clicked. Now, I'd like to achieve the same fun ...

Using CSS height 100% does not function properly when the content overflows

Here's what's going on with this code snippet: HTML <div class="one"> <div class="will-overflow"> </div> </div> CSS html, body { width: 100%; height: 100%; } .one { height: 100%; background: r ...

Looking for assistance with jqGrid

Is there a method to confirm if the grid has finished loading using a separate function? My goal is to automatically click on the first row and move that row to the second grid once this action occurs. jQuery("#search_grid").jqGrid('setGridParam&apo ...

Modifying the content within a DIV element

I want to make changes to my DIV. <div id="main"> <div id="one"> <div class="red"> ... </div> <img class="avatar" src="img/avatar1.jpg"/> <span class="name"> John < ...

The link button appears unselected without a border displayed

I am facing an issue with a link button in my code. Here is the snippet: <div class="col-2"> <a type="button" routerLink="auto-generate-schedules/generate" class="btn btn-primary mb-2">Generate Sche ...

Guide to building a robust tab system using JQuery and Ajax

I am currently working on a mockup page where the accordion feature is functioning well. However, I am looking to implement a tab-like system that will allow users to navigate between different pages. Specifically, I want users to be able to click on page ...

Using the index property within *ngFor in Angular versions 2, 4, and 5

When using Angular 2, 4, or 5, you cannot directly use $index. To utilize the index, you need to define it first. <li *ngFor="let item of items; let i = index">{{item}} - {{i}}</li> {{i}} represents the index of items However, in Angular ...

Loading JSON data in AngularJS before parsing and loading HTML content from it

Greetings, I am new to Angular and seeking some guidance. My goal is to limit a list of notifications to three using limitTo, and then load the rest when a button is clicked. Currently, I am unsure about the following: How to set up the "view" and appl ...

Which JavaScript library or template engine would be most suitable for this scenario?

I am tasked with creating an invite your Facebook friends module that will display the names and photos of your friends, allowing you to message them. It is essential that this feature seamlessly integrates into my website's design, so I need to style ...

I'm encountering issues with adding a div element using Jquery

I've been attempting to insert a div using jQuery, following the code example below. Unfortunately, it's not working as expected. jQuery: $('<div />', { $('<img />', { "src": "/Pages/Images/calendar.png").add ...

Reaching SVG with CSS

Is it possible to target SVG elements with CSS? In older versions of IE, they display like broken images and I want to hide them using modernizr. I'm looking for a solution like the following... .no-svg object[type=svg] { display:none; } I have be ...

The Javascript script is malfunctioning

Is there a way to modify the code so that the second drop-down menu becomes editable when an option is selected from the first drop down menu? The second drop-down menu should remain read-only if no option is selected, and specifically if "default" is ch ...

Struggling to set the value for a variable within an Angular factory?

When dealing with a variable as an array, I have no trouble pushing objects inside and retrieving the values within the controller. However, when trying to directly assign an object to that variable, I run into issues. If anyone can assist me in achieving ...

Is there a way to add Internet Explorer specific stylesheets to the Wordpress functions directory using enqueue?

I'm facing challenges setting up my Wordpress theme to be compatible with early versions of Internet Explorer. Most online tutorials have advised me to enqueue specific I.E stylesheets in the header, but this method has not been successful for me. Add ...