When you click on the collapsible header background, the content collapses, but the main icon does not update

I am currently using collapsible content from Bootstrap and I am encountering an issue with the icon not changing when I click on the collapsible-header background. Here is a visual representation of the problem:

1st part (not collapsed)

2nd part (After collapsed)

Upon clicking on the collapsible-header background, the lists collapse but the icon remains unchanged. I would like the icon to change along with the background upon clicking. However, if I click on the icon itself, it changes to a plus sign and collapses the contents as expected.

I want the collapsible header background to be responsive along with the icon. Unfortunately, the provided sample code does not visualize this properly. Please see below for the code snippet:

<div class="col-md-12" style="margin:20px 0 0 0">
    <ul class="collapsible collapsible-landing" data-collapsible="accordion">
        <li>
            <div class="collapsible-header">
                <span class="pull-left"><h3>Categories</h3></span>
                <span class="pull-right">
                    <a href="#catehide1" class="catehide2" id="catehide1"><i class="zmdi zmdi-plus"></i></a>
                    <a href="#cateshow1" class="cateshow2" id="cateshow1"><i class="zmdi zmdi-minus"></i></a>
                </span>
            </div>
            <div class="collapsible-body">
                <ul class="collapsible-list">
                    <li><a href="www.sample1.com">Sample 1</a></li>
                    <li><a href="www.sample1.com">Sample 2</a></li>
                    <li><a href="www.sample1.com">Sample 3</a></li>
                    <li><a href="www.sample1.com">Sample 4</a></li>
                    <li><a href="www.sample1.com">Sample 5</a></li>
                    <li><a href="www.sample1.com">Sample 6</a></li>
                    <li><a href="www.sample1.com">Sample 7</a></li>
                    <li><a href="www.sample1.com">Sample 8</a></li>
                    <li class="clearfix"></li>
                </ul>
            </div>
        </li>
    </ul>
</div>

Here is the CSS section related to this issue:

.catehide2, .cateshow2 {
    float: right;
    font-size: 20px;
    color: #000000;
    text-align: center;
    padding: 0;
}

.catehide2:hover, .cateshow2:hover {
    color: #9e9e9e;
    text-decoration: none;
    opacity: 1;
}

.categorybox1 ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    display: none;
    height: auto;
    font-family: 'Roboto', sans-serif;
    font-weight: 450;
    font-size: 15px;
    color: #424242;
    border: 0;
}

/* Additional CSS rules... */

Answer №1

UPDATED

Solution Using Pure CSS

The issue of icon change when expanding and collapsing the accordion has been fixed.

Feel free to view the example code on CODEPEN

HTML:

<div class="col-md-12 " style="margin:20px 0 0 0">
  <ul class="collapsible collapsible-landing">
    <li>
      <div class="panel-heading collapsed" data-toggle="collapse" data-target="#test">
        <h3 class="panel-title accordion-toggle">Categories</h3>

      </div>
      <div class="collapsible-body collapsible-body panel-collapse collapse" id="test">
        <ul class="collapsible-list">
          <li> <a href="www.sample1.com"> Sample 1 </a> </li>
          <li> <a href="www.sample1.com"> Sample 2 </a> </li>
          <li> <a href="www.sample1.com"> Sample 3</a> </li>
          <li> <a href="www.sample1.com"> Sample 4</a> </li>
          <li> <a href="www.sample1.com"> Sample 5</a> </li>
          <li> <a href="www.sample1.com"> Sample 6 </a> </li>
          <li> <a href="www.sample1.com">Sample 7</a> </li>
          <li> <a href="www.sample1.com">Sample 8 </a> </li>
          <li class="clearfix"></li>
        </ul>
      </div>
    </li>
  </ul>
</div>

CSS:

ul {
  list-style: none;
  margin: 0;
  padding: 0;
  height: auto;
  font-family: 'Roboto', sans-serif;
  font-weight: 450;
  font-size: 15px;
  color: #424242;
  border: 0;
}

.collapsible-landing {
  border-top: none;
  border-right: none;
  border-left: none;
  border-bottom: none;
  margin: 0.5rem 0 1rem 0;
  box-shadow: none;
}

.collapsible-list {
  background: #efefef;
}

.collapsible-list li a {
  width: 25%;
  padding: 10px 24px;
  float: left;
  color: #424242;
}

.collapsible-list li a:hover {
  text-decoration: none;
  border-bottom: none;
  color: #9e9e9e;
}

.collapsible-list li.clearfix {
  clear: both;
  padding: 0 0 20px 0;
}

.accordion-toggle:after {
  /* symbol for "opening" panels */      
  font-family: 'Material-Design-Iconic-Font';
  content: "\f273";
  float: right;
  color: inherit;
}

.panel-heading.collapsed .accordion-toggle:after {
  /* symbol for "collapsed" panels */
  content: "\f278";
}

Answer №2

Incorporating a small jQuery function can easily solve your problem without the need for extensive CSS coding. Give it a try and see if it works for you!

$(function(){
  $('#collapse1').on('hide.bs.collapse', function () {
    $('#button').html('<span class="glyphicon glyphicon-chevron-down"></span> Show');
  })
  $('#collapse1').on('show.bs.collapse', function () {
    $('#button').html('<span class="glyphicon glyphicon-chevron-up"></span> Hide');
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
  <h2>Collapsible Panel</h2>
  <div class="panel-group">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h4 class="panel-title">
          <a id="button" data-toggle="collapse" data-target="#collapse1">
            <span class="glyphicon glyphicon-chevron-down"></span> show
          </a>
        </h4>
      </div>
      <div id="collapse1" class="panel-collapse collapse">
        <div class="panel-body">Panel Body</div>
        <div class="panel-footer">Panel Footer</div>
      </div>
    </div>
  </div>
</div>

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

What is the best way to add the current date to a database?

code: <?php session_start(); if(isset($_POST['enq'])) { extract($_POST); $query = mysqli_query($link, "SELECT * FROM enquires2 WHERE email = '".$email. "'"); if(mysqli_num_rows($query) > 0) { echo '<script&g ...

When hovering over nav bar links, shadows are not displayed

When hovering over the navbar links, I am able to change their color but a shadow at the bottom is missing. Is there a way to add a shadow at the bottom like it had before when not hovered? Visit this link for more information ...

Styling the navigation bar using CSS and positioning a <div> element underneath

Struggling to design a navbar using Bootstrap and have a div positioned underneath it that fills the remainder of the page? You're not alone! As someone new to Bootstrap and CSS, this can be tricky. Here's an example of how I created a navbar: ...

"Creating Rounded Corners in HTML: A Step-by-Step Guide

Is there a way to round the corners of this image? Below is an excerpt from my body... <body> <h1 style="float:left; width:37%;"><font color="#99CC00"><font face="Verdana">MrEpicGaming4U</font></h1> <div style= ...

Pressing the reset button will restore the table to its original

As a new React developer with experience mainly in hooks, I have been struggling to find a good example involving hooks. Currently, I am working on implementing an antd table with search functionality. My question is, when a user types something into the ...

Issues with displaying form/button glyphicon when using a fixed table header in Bootstrap

I have a situation where I have a table with a fixed header. In one of the columns, there are delete buttons within a form. The table structure is as follows: <div class="panel panel-default table-responsive fixedHeader"> <!-- Table --> &l ...

Exploring the differences between translate3d and matrix in CSS 3 animations

Lately, my projects have demanded seamless transitions and animations. We've made the shift from using Javascript to CSS animations almost entirely. I've discovered that utilizing translate3d produces incredibly smooth animations on various devi ...

Tips for integrating CSS3 into Android WebView

Currently, I am working on an Android application that utilizes HTML5. My goal is to implement element rotation using CSS3 transformations. However, I need to ensure that the app remains compatible with Android 2.3. What would be the most effective metho ...

Attempting to ensure that my website is fully optimized for all devices and

I am currently facing an issue with my CSS code on iPad. I want to change the background-image displayed based on whether the user is using an iPad or not. Specifically, I want to use headerold.jpg as the background-image for iPads while keeping headernew. ...

CSS opacity is currently not working as intended

Why doesn't the opacity work here? I've tried using hex and rgba colors, but nothing changes. Even adding classes to tr and td didn't help... First attempt: .done{ background-color: rgba(0,175,51,5); color:white; opacity: 50; ...

The flex box structure crumbles, causing the boxes to align side by side instead of in a masonry layout

I'm currently facing a challenge with my project regarding the utilization of flexbox. Everything seems to be in order, but as you resize your viewport to around 1400px, the column layout collapses. I'm looking for a way to prevent this from hap ...

How can I capture the ng-click event in AngularJS when using bootstrap-select for styled select inputs?

After selecting an option, I am facing an issue where I cannot capture the ng-click event when a user chooses a value from the drop-down menu. This is because the select option has been altered by bootstrap-select, as the default select option does not sup ...

Having trouble with the functionality of JQuery drop feature?

As I delve into implementing drag and drop functionality with JQuery, I encounter a peculiar issue. I have set up 3 'draggable' divs and corresponding 3 'droppable' divs. The intended behavior is for each draggable element to be accepte ...

Can you tell me about the feature called "Inspect Browser" box?

Can you identify the significance of this enigmatic purple box while inspecting elements in the browser's developer tools? It does not correspond to padding, margins, or borders, yet it seems to be occupying space within the designated div. [1]: http ...

django is not able to load staticfiles from the statifiles_dirs directory

I have placed my style.css in the directory appname/static/appname/. In my settings.py, this is what I have: STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, "static/"), ) When I try to load it in my base.html like t ...

Is there a way to bring to life the addClass() and removeClass() jQuery functions through animation?

I am currently developing a website and I want to be able to toggle the visibility of sections by using the ".hidden" bootstrap class within click events. Here is the basic code snippet: $('selector').click(function(){ $('*part-to-hi ...

adjust the div's position based on the window's scrolling bars

Is there a way to make my div move down when the window scrolls down and up when the window scrolls up? How can I achieve this effect? ...

In IE8, a border is applied to the max-width property when the width is set to

I'm attempting to design a box that adjusts in size according to the parent container, utilizing a mix of max-width and width:100%. With this setup, the box's width is determined by the max-width property, ensuring it shrinks within its boundari ...

Display an array of objects using React within the columns of a Bootstrap grid

I am facing a challenge where I have an array of components that I need to render in separate cells. The number of elements in the array can vary, sometimes exceeding the limit of 12 cells in the Bootstrap grid system. In such cases, I need to create new r ...

Integrate a scrollbar seamlessly while maintaining the website's responsiveness

I encountered an issue where I couldn't add a scrollbar while maintaining a responsive page layout. In order to include a scrollbar in my datatables, I found the code snippet: "scrollY": "200px" However, this code sets the table size to 200 pixels r ...