Expand the specific column that is selected with just a click of the mouse

I am currently working with a layout featuring three columns that utilizes bootstrap. However, I have encountered an issue where clicking on one column results in the expansion of the adjacent columns as well. Unfortunately, I am unable to display the snippet code here, so I have provided an image showcasing the problem: https://i.sstatic.net/1aE2Q.png

I am seeking guidance on how to alter this behavior. My goal is to click on a single column and have only that specific column expand without affecting the others. Any suggestions on how I can achieve this?

$('li.submenu a[href="#"]').click(
  function(e) {
    e.preventDefault();
    $(this).toggleClass('open');
    $(this).next().toggle();
    $('.col-md-4').toggleClass('flex-last');
    return false;
  });
ul.ul_submenu {
  display: none;
}

.submenu a i {
  transition: ease .3s;
}

.submenu a.open i {
  transform: rotate(-90deg);
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://kit.fontawesome.com/a076d05399.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container-fluid">
  <div class="row">
     // Column 1 content...
     // Column 2 content...
     // Column 3 content...
     // Additional columns...
  </div>
</div>😉

Answer №1

The Bootstrap grid system utilizes Flexbox (I highly recommend learning more about it if you haven't already). Your current HTML structure is set up in a way that each row serves as an integral unit, enclosed within a div.row. You have created a single-line row consisting of three columns. When one of the child elements (in this case, your ul submenu) within the div.row expands, it doesn't just expand its siblings but enlarges the containing div (div.row). This expansion of the containing block pushes down the subsequent div.row, and this behavior cannot be altered due to the nature of how it works.

In order to achieve the desired functionality mentioned in your question, I suggest considering a different HTML structure: construct three columns, each comprising multiple rows. Here's a functional example below (execute in full-page mode). Please note that there might be some implications on the vertical stacking order, although it's likely not a significant issue.

$('li.submenu a[href="#"]').click(
  function(e) {
    e.preventDefault();
    $(this).toggleClass('open');
    $(this).next().toggle();
    $('.col-md-3').toggleClass('flex-last');
    return false;
  });
ul.ul_submenu {
  display: none;
}

.submenu a i {
  transition: ease .3s;
}

.submenu a.open i {
  transform: rotate(-90deg);
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://kit.fontawesome.com/a076d05399.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container-fluid">
  <div class="col-md-4">
    <div class="row">
      // Additional HTML code blocks
    </div>
    <div class="row">
      // Additional HTML code blocks
    </div>
    <div class="row">
      // Additional HTML code blocks
    </div>
  </div>
  // Additional column structures here
</div>

Answer №2

Utilizing indexing allows you to pinpoint the specific element you wish to manipulate:

let selectedElement = $(".yourClass")[0];

This code snippet will target the first element with the specified class, and you can adjust the index accordingly for other elements!

Answer №3

After reviewing the code provided, it appears that the potential error lies within this specific line:

$('.col-md-3').toggleClass('flex-last');

Essentially, this line targets all elements with the class col-md-3 and toggles the class flex-last for each of these elements (as per your explanation).

To refine this behavior and only affect the parent elements of the clicked element, you can modify the code as follows:

$(this).parents('.col-md-3').toggleClass('flex-last');

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

Using jQuery to Extract Data from an External JSON Source

I am working with a large 2 MB JSON object that I need to parse using jQuery. The entire object has been saved in a file called "timeline.js", and I am looking to extract specific records from it as required. Originally, my dataset was in XML format, but ...

Preventing div resizing in AngularJS through scroll functionality

I want to create dialog boxes that are draggable and resizable and can contain arbitrary HTML-formatted content. To achieve this, I am utilizing JQLite within a directive. The resizing functionality is triggered by a mousedown event on a div in the bottom ...

I am having issues with my JavaScript code, I could really use some assistance

Currently, I am developing a custom file search program. The goal is to have a textarea where users can input text, which will then generate a clickable link below it. However, I am facing issues with the current implementation. Below is the snippet of my ...

The JavaScript else statement is failing to execute as intended

Hello, I am new to JavaScript and could really use some assistance. In the code snippet below, I am having trouble with the logic for the submit button. The line _btn.addEventListener seems to be causing an issue where only the "if" part is being executed ...

How can I ensure that all the text is always in lowercase in my Angular project?

Is there a way to ensure that when a user enters text into an input field to search for a chip, the text is always converted to lowercase before being processed? Currently, it seems possible for a user to create multiple chips with variations in capitaliza ...

Can the DNS cached entry in Firefox be updated and changed?

I have a specific testing requirement to meet. One of the requirements is to redirect a non-existent URL to a specific IP address, which is what the DNS is currently handling. I suspect that Firefox is using an internal DNS cache. However, I'm having ...

What is the process of linking this JavaScript code to an outer stylesheet and integrating it with the HTML document?

After encrypting an email address using Hiveware Enkoder, the result can be found below. obfuscated code I typically include it directly inside the div as is. However, I would like to streamline my code by placing it in an external file. While I know ho ...

Ways to position a button in the center of an image

Just a quick query about HTML and CSS. How can I position a button on top of an image in the center? Here is the current code: .button-link{ text-decoration:none !important; color:white; } .button-td{ background:#FFA06A; text-align:center; } ...

i am trying to execute a function in an angularjs controller through html, but the scope variable that i am

Being new to angularjs, I'm looking to understand the process of calling a function defined in a controller from HTML within angularjs. I have written the following code in my HTML: First.html <input type="text" ng-model="model.name" name="name ...

Issue with submitting the form... Pressing the Enter key

I'm currently facing an issue with a form that contains drop-down lists, a text field, and a button... Upon clicking the button, an AJAX function is triggered... which then calls a PHP function to fetch results from a MySQL database... The problem a ...

The combination of useEffect and Client component has the power to greatly

As a newcomer to React development, I've encountered a blocking error in my code that isn't being detected by Visual Studio Code. Here's the code snippet for my NavBar component: import React, { useState, useEffect } from "react"; import Ima ...

Exploring the capabilities of HTML5's file API along with Octokit.js to work with

I have been trying to create a form that allows users to upload binary data to GitHub using octokit.js. However, every time I attempt to do so, the data ends up corrupted on the GitHub side. Here is a minimal working example: http://jsfiddle.net/keddie/7r ...

Understanding how types intersect in TypeScript

I'm currently diving into Type Relations in TypeScript. Can someone help explain what happens when we intersect the types represented by these two expressions: {a:number}[] & {b:string}[] Does this result in {a:number, b:string}[] ? Any clarificat ...

The attribute "value" for Material-UI autocomplete cannot be used in conjunction with the "getOptionLabel" attribute

<Autocomplete id="license-select" options={licReqList} value = {licReqList[0] ? licReqList[0].licReqStr : null} getOptionLabel={(option) => option.licReqStr} onChange={ha ...

Map does not provide zero padding for strings, whereas forEach does

Currently working on developing crypto tools, I encountered an issue while attempting to utilize the map function to reduce characters into a string. Strangely enough, one function works perfectly fine, while the other fails to 0 pad the string. What could ...

Utilizing AJAX for fetching a partial view

My Web Forms legacy project had a feature where a panel would be displayed when the user clicked on a button. Now, I am working on rebuilding this functionality in C# MVC. The new view will utilize Javascript and AJAX to display a partial view as needed. ...

Setting a global variable in the JavaScript code below

Is there a way to make the modal variable global by setting it as var modal = $("#modal"); ? The code snippet below includes the modal variable and is not functioning properly. It needs to work correctly in order to display: "Hello name, You have signed u ...

Vue warning: Issue encountered in created hook - Attempting to access property 'get' of an undefined variable is causing a TypeError

I encountered an error while using axios: [Vue warn]: Error in created hook: "TypeError: Cannot read property 'get' of undefined" export default { methods: { loadUsers(){ axios.get("api/user").then(data => ...

Rearrange the position of an element within an array

I'm struggling to find a solution for rearranging elements within an array. Consider the following: var array = [ 'a', 'b', 'c', 'd', 'e']; How can I create a function that moves the element 'd&a ...

What is causing the CSS loader to continue showing up even after all the items have finished loading?

I have been developing an innovative newspaper/blogging platform using CodeIgniter 3.1.8 and Twitter Bootstrap 4. Currently, my focus is on implementing the AJAX functionality to load more posts dynamically. The default setup paginates the posts, display ...