Display the collapsible panel and modify the class of the other panel

I am trying to implement a collapse panel with a button and adjust the class of another panel so that it fits within the width of the page. Here is the code snippet I have been working on:

button

// first attempt
$('#signup').on('click', function() {
  $('#table').removeClass('col-md-12').addClass('col-md-8');
});
// second attempt
$('#signup').on('click', function() {
  $('#table').toggleClass('col-md-12 col-md-8');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn btn-success" data-toggle="collapse" data-target="#panel" id="signup">Signup</button>

<div class="form-row">
  <!-- panel 1 -->
  <div class="form-group col-md-4 collapse" id="panel">
    // content goes here
  </div>

  <!-- panel 2 -->
  <div class="form-group col-md-12 ml-auto" id="table">
    // content goes here
  </div>
</div>

Upon clicking the button, the aim is to display panel 1 while simultaneously changing the class of panel 2 from 'col-md-12' to 'col-md-8' in order to align it correctly within the bootstrap grid system. However, my attempts using an onclick trigger have proven unsuccessful.

Answer №1

$('#panel').on('hidden.bs.collapse', function(e) {
  $('#table').toggleClass('col-xs-12').toggleClass('col-xs-8');
}).on('show.bs.collapse', function(e) {
  $('#table').toggleClass('col-xs-8').toggleClass('col-xs-12');
})
.form-row.row {
  margin-right: 0px;
  margin-left: 0px;
}
<header>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
</header>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<button class="btn btn-success" data-toggle="collapse" data-target="#panel" id="signup">Signup</button>

<div class="form-row row">
  <!-- panel 1 -->
  <div class="form-group col-xs-4 collapse" id="panel">
    // content goes here for Panel 1
  </div>

  <!-- panel 2 -->
  <div class="form-group col-xs-12 ml-auto" id="table">
    // content goes here for Panel 2
  </div>
</div>

Edit

An updated snippet that does not use the Bootstrap grid system.

$('#panel').on('hidden.bs.collapse', function(e) {
  $('#table').toggleClass('col-xs-12');
}).on('show.bs.collapse', function(e) {
  $('#table').toggleClass('col-xs-12');
})
.some-sontainer {
  margin-right: 0px;
  margin-left: 0px;
  display: flex;
}

.some-sontainer .col1{
  width: 200px;
}

.some-sontainer .col2{
  width: 100%;
}

#table{
  background-color: red;
}

#panel{
  background-color: green;
}
<header>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
</header>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<button class="btn btn-success" data-toggle="collapse" data-target="#panel" id="signup">Signup</button>

<div class="form-row some-sontainer">
  <!-- panel 1 -->
  <div class="form-group col1 collapse" id="panel">
    // content goes here for Panel 1
  </div>

  <!-- panel 2 -->
  <div class="form-group col2 col-xs-12 ml-auto" id="table">
    // content goes here for Panel 2
  </div>
</div>

Answer №2

After experimenting with different approaches, I successfully integrated the method.

$('#panel').on('hidden.bs.collapse', function(){
    $('#table').removeClass('col-md-8').addClass('col-md-12');
});
$('#signup').on('click', function(){
    $('#table').removeClass('col-md-12').addClass('col-md-8');
});

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

Bootstrap failing to enclose columns that do not have a specified size

As I dive into creating a Bootstrap grid website to expand my knowledge, I am following the guidelines provided here. Trying to replicate the examples from that page has led me to the first hurdle. The issue arises when dealing with columns without specif ...

Opening a Partial View (.ascx) in jQuery UI Dialog

I'm a bit confused about how to display a partial view in a jQuery UI Dialog window. I was initially using a simple Dialog but decided to explore other options for various reasons. Most online tutorials only demonstrate opening <div></div> ...

Ensure that a select input, dynamically generated, is marked as 'required' only when other inputs have been filled out

Displayed below is a snapshot of a page I'm developing that allows users to input details about various rooms in a property. Users have the ability to 'add' multiple rooms, triggering javascript/jQuery to dynamically create additional input ...

Tips for aligning the dialog box in the center of the screen based on the current scroll position

Is there a way to center the dialog box vertically at the current scroll position of the window when any of the "show dialog" buttons are clicked? For instance, if I click on location 3, I want the dialog box to be centered vertically within the current v ...

Learn the process of playing a video in an HTML5 video player after it has finished downloading

// HTML video tag <video id="myVideo" width="320" height="176" preload="auto" controls> <source src="/server/o//content/test2.mp4" onerror="alert('video not found')" type="video/mp4"> Your browser does not support HTML5 vid ...

Issue with background image resizing when touched on mobile Chrome due to background-size property set to cover

My current challenge involves setting a background image for a mobile page using a new image specifically designed for mobile devices. The image is set with the property background-size: cover, and it works perfectly on all platforms except for mobile Chro ...

Verify a group of website links for the presence of a specific table identification

My collection of URLs includes some with tables embedded and others without any table content whatsoever. Upon examining the source code, I noticed a specific table ID that is loaded dynamically. Is there a convenient script or tool available to scan each ...

I am looking to send a combination of string and HTML data to the controller when using the Summernote editor in MVC

As a beginner in MVC development, there are certain aspects that I am still struggling with. Currently, I am using the Summernote Editor to compose blog posts and Ajax to submit them. However, I encountered an issue when trying to send both HTML data from ...

Issue with the Textualizer effect in HTML not functioning properly

Having trouble with the textualizer effect - my screen is just blank. I've tried copying it from this source: . Inserted it into both visual studio and notepad, but still no success! <html> <head> <title></title> <script sr ...

Ways to guide users through a single-page website using the URL bar

I currently have a one-page website with links like <a href="#block1">link1</a>. When clicked, the browser address bar displays site.com/#block1 I am looking to modify this so that the browser shows site.com/block1, and when the link is clicke ...

In jQuery, a dropdown selection can be filled with multiple textboxes sharing the same class

I'm experimenting with the idea of using multiple textboxes with the same class filled with different dropdown options that also have the same class. However, I am encountering some issues. When I click on a dropdown option, it changes the value in a ...

What measures can be taken to safeguard my web app from unauthorized devices attempting to connect?

I am looking for a way to restrict access to a webapp so that only authorized users can connect from approved computers or mobile devices. If a user enters the correct username and password, access will be granted only if they are using a device approved b ...

What is the process for integrating php script within javascript?

Is it possible to incorporate php scripts into javascript[1] in the same manner as it can be done in html[2]? server.php: <?php echo 'hello World'; ?> client.js (or client.php if needed): function foo() { var i = <?php include ...

Struggling with organizing the layout of your HTML page? Let us lend

I am having difficulty with the layout of a page I'm working on. The lower page navigation is not positioning properly - it wraps below the left column instead of staying below the data columns. Additionally, the border for the navigation appears at t ...

A guide on how to stop the loading animation and transition to a different slide using HTML and CSS

Check out this CSS code snippet @import url(https://fonts.googleapis.com/css?family=Quattrocento+Sans); @import url(https://fonts.googleapis.com/css?family=Quattrocento+Sans); .loading { position: fixed; top: 0; left: 0; width: 100%; height: ...

Switching from the jQuery .it() function to the Angular framework

I am looking to convert this code to Angular. I have already written some of the code, but I am unsure what to use in place of :It(). The jQuery code can be found at the following URL: jsfiddle.net Here is my Angular code: x = 3; $scope.itemsPerPage = 3; ...

Utilizing jQuery to consistently position an element next to another regardless of its properties

Discovering this website has been a delight. As a novice programmer, I am struggling to grasp the concept of positioning one element next to another regardless of the preceding element. For instance, If Element A can hold any value, And if Element B resem ...

"AngularJS directive mandating the use of the required attribute for internal control

I've encountered a challenge with this specific issue. We are using a directive called deeplink, which contains the following code: restrict: 'E', require: 'ngModel', scope: { smDropdown: '=smDeeplinkDropdown', s ...

Adjust the content within an HTML div by utilizing AngularJS

Snippet of AngularJs code to display different content based on selection: <select> <option value="0">--Select--</option> <option value="1">Individual</option> <option value="2"> ...

A sleek and modern fixed navigation bar in Bootstrap 4 featuring two elegantly stacked rows, with a striking brand image perfectly centered

I need help creating a Bootstrap 4 fixed navigation bar with 2 rows. The first row should have a centered brand image like the one on (with the image size changing after scrolling down), and the second row should contain the navigation menu. Thank you i ...