0

I'm trying to build a list with subpoints that collapse on command using the bootstrap collapse. This has worked fine, but I need to add an arrow that points right when the collapse is up, and down when it's dropped down.

This is my HTML...

<ul>
<li data-toggle="collapse" data-target="#Sub1">List Item One <em class="glyphicon glyphicon-chevron-right"></em><em class="glyphicon glyphicon-chevron-down"></em>
<ul class="collapse" id="Sub1">
<li>Sub-item</li>
</ul>
<li>List Item Two</li>
</ul>

Currently I'm using this CSS to modify it...

.collapse {display: none;}
.collapse.in {display: block;}
.collapsed > .glyphicon .glyphicon-chevron-down {display: inline-block;}
.collapsed > .glyphicon .glyphicon-chevron-right {display: none;}

http://jsfiddle.net/ddx0c3c2/

The problem I'm encountering is that both icons are displaying, and not disappearing when the class changes. Is it possible to do this with just CSS, or do I need to use jQuery?

4
  • 1
    where is .collapsed ?
    – BReal14
    Oct 1, 2014 at 20:07
  • It gets added to the parent list item when the collapse is clicked, though my fiddle doesn't reflect that unless you are inspecting element in Chrome. I may be using the wrong class to modify...
    – Maddi
    Oct 1, 2014 at 20:09
  • @Maddi there is nothing here which applies the .collapsed class. dynamically applying classes in response to a click can't be done with HTML and CSS alone. Oct 1, 2014 at 20:24
  • In default mode (first load), .collapsed is not on the list and thus is not addressable until it's been click-loaded.
    – BReal14
    Oct 1, 2014 at 20:26

2 Answers 2

1

Here you are: JSFiddle. I've solved it with jQuery.

Your first "mistake" was to have a false CSS-Selector order. Your glyphicon isn't inside the .collapse class.

HTML:

<ul>
    <li data-toggle="collapse" data-target="#Sub1">List Item One <em class="glyphicon glyphicon-chevron-right arrow_show"></em><em class="glyphicon glyphicon-chevron-down"></em>

        <ul class="collapse" id="Sub1">
            <li>Sub-item</li>
        </ul>
        <li>List Item Two</li>
</ul>

CSS:

.collapse {
  display: none;
}

.collapse.in {
  display: block;
}

.glyphicon {
    display:none;
}
.arrow_show {
    display: inline;
}

JS:

$('li[data-target="#Sub1"]').on("click", function() {
  $('.glyphicon-chevron-right').toggleClass('arrow_show');
  $('.glyphicon-chevron-down').toggleClass('arrow_show');
 })
1
  • I would but my reputation is too low...newbie troubles. :) If I may somewhat add to my question, how would I go about setting up that bit of jQuery if I have mutiple independant list items with dropdowns? Do I have to include it for each individual time I use the feature?
    – Maddi
    Oct 1, 2014 at 21:33
0

If set up correctly, you don't need jQuery, you just need to set up your css:

DEMO: http://jsbin.com/peqihe/2/edit

enter image description here

CSS -- these are my styles for making it better looking, it's a bit much.

 .panel-group, 
 .panel-group .panel, 
 .panel-group .panel-heading,
 .panel-group .panel-heading a,  
 .panel-group .panel-title,  
 .panel-group .panel-title a,  
 .panel-group .panel-body,
 .panel-group .panel-group .panel-heading + .panel-collapse > .panel-body  {
    border-radius:2px;
    border:0;
}
.panel-group .panel-heading + .panel-collapse > .panel-body {
  border-top: 0;
}


 .panel-group .panel-heading {
    padding: 0;
}
 .panel-group .panel-heading a {
    display: block;
    background: #668bb1;
    color:#fff;
    padding:15px;
     text-decoration:none;
   position: relative;
}
 .panel-group .panel-heading a.collapsed {
    background: #c4ced5; 
    color:inherit;
}

 .panel-group .panel-heading a:after {
    content: '-';
    position: absolute;
    right: 20px;
}
 .panel-group .panel-heading a.collapsed:after {
    content: '+';
}
 .panel-group .panel-collapse {
    margin-top:5px!important;
}
 .panel-group .panel-body  {
    background: #f7f7f7;
    padding:15px;
}
 .panel-group .panel {
  background-color: transparent;
}

.panel-group .panel-body p:last-child,
.panel-group .panel-body  ul:last-child,
.panel-group .panel-body  ol:last-child {
  margin-bottom: 0;
}

HTML

<div class="container">

         <div class="panel-group" id="accordion">

            <div class="panel panel-default">
               <div class="panel-heading">
                  <h4 class="panel-title">
                     <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
                     Collapsible Group Item #1
                     </a>
                  </h4>
               </div><!--/.panel-heading -->
               <div id="collapseOne" class="panel-collapse collapse in">
                  <div class="panel-body">
                     Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
                  </div><!--/.panel-body -->
               </div><!--/.panel-collapse -->
            </div><!-- /.panel -->

            <div class="panel panel-default">
               <div class="panel-heading">
                  <h4 class="panel-title">
                     <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
                     Collapsible Group Item #2
                     </a>
                  </h4>
               </div><!--/.panel-heading -->
               <div id="collapseTwo" class="panel-collapse collapse">
                  <div class="panel-body">
                     Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
                  </div><!--/.panel-body -->
               </div><!--/.panel-collapse -->
            </div><!-- /.panel -->

            <div class="panel panel-default">
               <div class="panel-heading">
                  <h4 class="panel-title">
                     <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
                     Collapsible Group Item #3
                     </a>
                  </h4>
               </div><!--/.panel-heading -->
               <div id="collapseThree" class="panel-collapse collapse">
                  <div class="panel-body">
                     Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
                  </div><!--/.panel-body -->
               </div><!--/.panel-collapse -->
            </div><!-- /.panel -->
         </div><!-- /.panel-group -->

</div><!-- /.container -->  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.