Using toggleClass to resize Bootstrap columns

Dilen Pat
Dilen Pat used Ask the Experts™
on
I have a webpage which has two main divs. One for a small 'control panel' on the left (takes up a quarter of the space), and a larger main table on the right (takes up three quarters of space). To accommodate different screen sizes, I've incorporates boostrap columns e.g.                 <div className="control-panel col-lg-3 col-md-3 col-sm-3 col-xs-3" id="control-panel">

This mostly works. If I dynamically resize the browser, this is as expected.

Moving on, I've created a toggle button whose click event hides this control panel div (and is expected to resize the main table from 9 columns to the full 12 columns).

Right now for the resizing of the main table, in the click function i just have:
 $('#theMainTable').toggleClass('col-lg-12 col-lg-9');

Open in new window

                                                           
This will toggle between 9 column width and 12, but this will only work on a large screen that isn't resized.
If I dynamically resize the browser to anything else, this will not work.

My assumption is I need some way to toggle multiple screen combos like:
 $('#theMainTable').toggleClass('[b]{col-lg-12 col-md-12 col-sm-12 col-xs-12}[/b] {col-lg-9 col-md-9 col-sm-9 col-xs-9}');

Open in new window

(although the above seems to only work for small resolutions)


the best i can try is:
		$('#theMainTable').toggleClass('col-lg-12 col-lg-9');
		$('#theMainTable').toggleClass('col-md-12 col-md-9');
		$('#theMainTable').toggleClass('col-sm-12 col-sm-9');
		$('#theMainTable').toggleClass('col-xs-12 col-xs-9');

Open in new window


but this doesnt work dynamically. i.e. it works for any resolution you resize to, so long as you refresh the page first

any help?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Don't worry about the md sm and lg styles - just do xs - seeing as they are all the same 12:9 it makes no difference
CSS
<style type="text/css">
.master-panel {
  background: blue; 
  color: white;
  padding: 20px;
}
.control-panel {
  background: red; 
  color: white;
  padding: 20px;
}
</style>

Open in new window

HTML
  <div class="row">
    <div class="col-xs-9 master-panel">
      I am the master
    </div>
    <div class="col-xs-3 control-panel">
      I am the control panel
    </div>
  </div>
  <button class="btn btn-primary">Toggle</button>

Open in new window

jQuery
<script>
$(function() {
  $('button').click(function() {
    $('.control-panel').toggle();
    $('.master-panel').toggleClass('col-xs-9 col-xs-12');
  });
});
</script>

Open in new window

Working sample here

Author

Commented:
What would need to be done if they're not all 12:9? For example, stretching the browser too much (e.g. over two screens) makes the control panel look too large and bulky so for lg it may need to be reduced to 2 or 1
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Then we would probably need to look at a solution that adds and removes classes manually rather than using toggle class.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
I've seen example code that replaces toggleclass with some sort of add/removeclass. They're usually used in conjunction with if statements. But then surely that would be no different to my previous example? It won't work when dynamically resizing the browser window. It'll only work when you stop resizing the window and then refresh the page

            
  
                $('#theMainTable').toggleClass('col-lg-12 col-lg-9');
		$('#theMainTable').toggleClass('col-md-12 col-md-9');
		$('#theMainTable').toggleClass('col-sm-12 col-sm-9');
		$('#theMainTable').toggleClass('col-xs-12 col-xs-9');

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
That code is only toggling -12 and -9 - in which case my earlier solution solves the problem. But you have stated you want to toggle to different classes on different screens. That is when toggleClass is not going to work.

In other words - if the rules are the same for all screens then you only need the col-xs-12 and col-xs-9 - you don't have to worry about the others.

If you want to bring in col-lg-10 and col-lg-2 as well - that is when you complicate the issue and we have to move to add / remove class.

Author

Commented:
it seems the toggle class originally used was actually working, but a JQuery somewhere else was interfering. Once I commented it out, it worked. Thanks for the help
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial