Jacob J Jacob J - 1 month ago 6
Javascript Question

Can I change bootstrap collapse to only show/expand when clicking an icon?

I'm making a sort of select box where they can choose categories. I'd like it so they can choose whichever item. To do this, I need the items to not show/hide when clicking anywhere, only when clicking on the + or - icons. Hopefully that makes sense. I tried moving the data-toggle="collapse" thinking i could just put that on my icon, but it ended up breaking.



.list-group.list-group-root {
padding: 0;
overflow: hidden;
}

.list-group.list-group-root .list-group {
margin-bottom: 0;
}

.list-group.list-group-root .list-group-item {
border-radius: 0;
border-width: 1px 0 0 0;
font-size: 18px;
}

.list-group.list-group-root > .list-group-item:first-child {
border-top-width: 0;
}

.list-group.list-group-root > .list-group > .list-group-item {
padding-left: 30px;
font-size: 16px;
}

.list-group.list-group-root > .list-group > .list-group > .list-group-item {
padding-left: 45px;
font-size: 14px;
}

.list-group-item .glyphicon {
margin-right: 5px;
}

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>

<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"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div class="list-group list-group-root well">

<a href="#item-1" class="list-group-item" data-toggle="collapse">
<i class="fa fa-plus pull-right"></i>Item 1
</a>
<div class="list-group collapse" id="item-1">

<a href="#item-1-1" class="list-group-item" data-toggle="collapse">
<i class="fa fa-plus pull-right"></i>Item 1.1
</a>
<div class="list-group collapse" id="item-1-1">
<a href="#" class="list-group-item">Item 1.1.1</a>
<a href="#" class="list-group-item">Item 1.1.2</a>
<a href="#" class="list-group-item">Item 1.1.3</a>
</div>

<a href="#item-1-2" class="list-group-item" data-toggle="collapse">
<i class="fa fa-plus pull-right"></i>Item 1.2
</a>
<div class="list-group collapse" id="item-1-2">
<a href="#" class="list-group-item">Item 1.2.1</a>
<a href="#" class="list-group-item">Item 1.2.2</a>
<a href="#" class="list-group-item">Item 1.2.3</a>
</div>

<a href="#item-1-3" class="list-group-item" data-toggle="collapse">
<i class="fa fa-plus pull-right"></i>Item 1.3
</a>
<div class="list-group collapse" id="item-1-3">
<a href="#" class="list-group-item">Item 1.3.1</a>
<a href="#" class="list-group-item">Item 1.3.2</a>
<a href="#" class="list-group-item">Item 1.3.3</a>
</div>

</div>

<a href="#item-2" class="list-group-item" data-toggle="collapse">
<i class="fa fa-plus pull-right"></i>Item 2
</a>
<div class="list-group collapse" id="item-2">

<a href="#item-2-1" class="list-group-item" data-toggle="collapse">
<i class="fa fa-plus pull-right"></i>Item 2.1
</a>
<div class="list-group collapse" id="item-2-1">
<a href="#" class="list-group-item">Item 2.1.1</a>
<a href="#" class="list-group-item">Item 2.1.2</a>
<a href="#" class="list-group-item">Item 2.1.3</a>
</div>

<a href="#item-2-2" class="list-group-item" data-toggle="collapse">
<i class="fa fa-plus pull-right"></i>Item 2.2
</a>
<div class="list-group collapse" id="item-2-2">
<a href="#" class="list-group-item">Item 2.2.1</a>
<a href="#" class="list-group-item">Item 2.2.2</a>
<a href="#" class="list-group-item">Item 2.2.3</a>
</div>

<a href="#item-2-3" class="list-group-item" data-toggle="collapse">
<i class="fa fa-plus pull-right"></i>Item 2.3
</a>
<div class="list-group collapse" id="item-2-3">
<a href="#" class="list-group-item">Item 2.3.1</a>
<a href="#" class="list-group-item">Item 2.3.2</a>
<a href="#" class="list-group-item">Item 2.3.3</a>
</div>

</div>


<a href="#item-3" class="list-group-item" data-toggle="collapse">
<i class="fa fa-plus pull-right"></i>Item 3
</a>
<div class="list-group collapse" id="item-3">

<a href="#item-3-1" class="list-group-item" data-toggle="collapse">
<i class="fa fa-plus pull-right"></i>Item 3.1
</a>
<div class="list-group collapse" id="item-3-1">
<a href="#" class="list-group-item">Item 3.1.1</a>
<a href="#" class="list-group-item">Item 3.1.2</a>
<a href="#" class="list-group-item">Item 3.1.3</a>
</div>

<a href="#item-3-2" class="list-group-item" data-toggle="collapse">
<i class="fa fa-plus pull-right"></i>Item 3.2
</a>
<div class="list-group collapse" id="item-3-2">
<a href="#" class="list-group-item">Item 3.2.1</a>
<a href="#" class="list-group-item">Item 3.2.2</a>
<a href="#" class="list-group-item">Item 3.2.3</a>
</div>

<a href="#item-3-3" class="list-group-item" data-toggle="collapse">
<i class="fa fa-plus pull-right"></i>Item 3.3
</a>
<div class="list-group collapse" id="item-3-3">
<a href="#" class="list-group-item">Item 3.3.1</a>
<a href="#" class="list-group-item">Item 3.3.2</a>
<a href="#" class="list-group-item">Item 3.3.3</a>
</div>

</div>

</div><!-- end category list -->




Answer

this is a way to do it..

(as explained by @Justin L.)

.list-group.list-group-root {
    padding: 0;
    overflow: hidden;
}

.list-group.list-group-root .list-group {
    margin-bottom: 0;
}

.list-group.list-group-root .list-group-item {
    border-radius: 0;
    border-width: 1px 0 0 0;
    font-size: 18px;
}

.list-group.list-group-root > .list-group-item:first-child {
    border-top-width: 0;
}

.list-group.list-group-root > .list-group > .list-group-item {
    padding-left: 30px;
    font-size: 16px;
}

.list-group.list-group-root > .list-group > .list-group > .list-group-item {
    padding-left: 45px;
    font-size: 14px;
}

.list-group-item .glyphicon {
    margin-right: 5px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>

<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"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

 <div class="list-group list-group-root well">
    
      <span class="list-group-item" >
        <a  href="#item-1" data-toggle="collapse"><i class="fa fa-plus pull-right"></i></a> 1
    </span>
    <div class="list-group collapse" id="item-1">

        <span class="list-group-item" >
            <a  href="#item-1-1" data-toggle="collapse"><i class="fa fa-plus pull-right"></i></a>Item 1.1
        </span>
        <div class="list-group collapse" id="item-1-1">
            <a href="#" class="list-group-item">Item 1.1.1</a>
        </div>
    
 </div><!-- end category list -->