What is the best way to incorporate negative/positive symbols into Bootstrap collapsible panels?

Currently, I am utilizing Bootstrap and have implemented collapse panels in my project. I would like to add plus and minus icons to these panels.

I have managed to achieve this by using a code snippet that I found on Stack Overflow (I will include the link later). The code I used requires duplicating it for each group of collapse panels as they are assigned unique IDs. Is there a method to make it universal so that it only needs to be inserted once and will function across multiple panels on the same page?

jQuery(function ($) {
  var $active = $('#accordionthree .panel-collapse.in').prev().addClass('active');
  $active.find('.symbol').append('<span class="glyphicon glyphicon-minus pull-left"></span>');
  $('#accordionthree .panel-heading').not($active).find('.symbol').prepend('<span class="glyphicon glyphicon-plus pull-left"></span>');
  $('#accordionthree').on('show.bs.collapse', function (e)
  { 
    $('#accordionthree .panel-heading.active').removeClass('active').find('.glyphicon').toggleClass('glyphicon-plus glyphicon-minus'); 
    $(e.target).prev().addClass('active').find('.glyphicon').toggleClass('glyphicon-plus glyphicon-minus'); 
  });
  $('#accordionthree').on('hide.bs.collapse', function (e) 
  { 
         $(e.target).prev().removeClass('active').find('.glyphicon').removeClass('glyphicon-minus').addClass('glyphicon-plus'); 
  });
}); 

Answer №1

Here is a CSS solution :

.panel-heading a:after {
    font-family:'Glyphicons Halflings';
    content:"\2212";
    float: right;
    color: grey;
}
.panel-heading a.collapsed:after {
    content:"\2b";
}

This solution is based on the collapse example in Bootstrap Collapse accordion example and incorporates font-awesome.

Hope you find this helpful.


.panel-heading a:after {
  font-family:'Glyphicons Halflings';
  content:"\2212";
  float: right;
  color: grey;
}
.panel-heading a.collapsed:after {
  content:"\2b";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="panel-group" id="accordion">
    <div class="panel panel-default" id="panel1">
        <div class="panel-heading">
             <h4 class="panel-title">
        <a data-toggle="collapse" data-target="#collapseOne" 
           href="#collapseOne">
          Collapsible Group Item #1
        </a>
      </h4>

        </div>
        <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>
        </div>
    </div>
    <div class="panel panel-default" id="panel2">
        <div class="panel-heading">
             <h4 class="panel-title">
        <a data-toggle="collapse" data-target="#collapseTwo" 
           href="#collapseTwo" class="collapsed">
          Collapsible Group Item #2
        </a>
      </h4>

        </div>
        <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>
        </div>
    </div>
    <div class="panel panel-default" id="panel3">
        <div class="panel-heading">
             <h4 class="panel-title">
        <a data-toggle="collapse" data-target="#collapseThree"
           href="#collapseThree" class="collapsed">
          Collapsible Group Item #3
        </a>
      </h4>

        </div>
        <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>
        </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

Issue: Unable to access GET request with Express and handlebars

Hello everyone, I'm just getting started with JS/Handlebars and I'm facing an issue trying to display an image from my home.hbs file in VS Code. When I start the server, this is the message I receive: https://i.sstatic.net/wUxB7.jpg Below is th ...

Issue with the react-native-autocomplete-input where the list does not close after selecting an option

After selecting an item from the list in my react-native-autocomplete-input, the list does not close. let Location = (props) => ( <View style={styles.formItem}> <Autocomplete data={props.autocompleteResults.predictions} de ...

Issue encountered in TypeScript: Property 'counter' is not found in the specified type '{}'.ts

Hey there, I'm currently facing an issue while trying to convert a working JavaScript example to TypeScript (tsx). The error message I keep encountering is: Property 'counter' does not exist on type '{}'.ts at several locations wh ...

Enhancing File Upload functionality with Struts 2 and jQuery

As a newbie to Struts2, I am attempting to upload a file in Struts 2 using the Struts2 jQuery plugin. My goal is to avoid page refresh during the file upload process. I understand that Struts 2 jQuery is supposed to facilitate AJAX requests to the action w ...

Incorporating Dynamic Event Handling in JavaScript, without the use of jQuery

Check out this code snippet: <html> <head> <script type="text/javascript> window.onload = function(){ sel = document.getElementsByTagName('select'); for(i=0;i<sel.length;i++)sel[i].onc ...

Having trouble opening or closing the nav menu with a click?

I've been struggling to get my hamburger menu to work properly for a while now. I've tried various solutions, but it seems like I just can't get it to open (Menu not opening is the issue :( ), The tutorial I followed to create this menu can ...

Ongoing Activity Triggered by jquery.blur() Function Currently Underway

I am experiencing a peculiar behavior with an input field: <input type="text" id="myId" ns-validate="" class="someClass" ng-model="Date" ng-focus="focus($event, 'Parameters')" ng-blur="blur($event)" tabindex="-1"> The focus event is linke ...

Discover the file system format using Node.js

Currently diving into a node js project and seeking ways to determine the file system type of a disk, such as FAT or NTFS. Can anyone provide guidance on how to achieve this using node js? Any assistance would be greatly valued. Thank you. ...

Dealing with Sideways Overflow Using slideDown() and slideUp()

Attempting to use slideUp() and slideDown() for an animated reveal of page elements, I encountered difficulty when dealing with a relatively positioned icon placed outside the element. During these animations, overflow is set to hidden, resulting in my ico ...

Display hidden text upon clicking using React Material UI Typography

I have a situation where I need to display text in Typography rows, but if the text is too long to fit into 2 rows, ellipsis are displayed at the end. However, I would like to show the full text when the user clicks on the element. I am attempting to chang ...

Click to load profile when searching

I have been working on an ajax search feature and I am trying to figure out how to load a user's profile when their name is clicked in the search results using ajax. I attempted to assign the user's ID from the database to a div element using the ...

Code for implementing vertical and horizontal scroll bars in a Kindo grid using Selenium WebDriver in the Eclipse IDE

Is there a way to click on a link within a Kindo grid that will redirect me to the appropriate page? I find that I need to scroll vertically to the right in order to locate the link item and click on it. The script I have tried using doesn't seem to w ...

Why does jQuery function properly in jsfiddle, but not in an HTML file?

I have been troubleshooting repeatedly, but I am unable to figure out why the jQuery code is not functioning as expected. Strangely enough, it works perfectly in jsfiddle! Here is the HTML code: <!doctype html> <html> <head> <meta ch ...

React useState is not updating the onClick event as expected

I'm facing a challenge with the code I'm currently working on. The issue arises when trying to handle controlled inputs and passing API responses to a child component, resulting in only rendering the last value. Below is the portion of the code ...

What is the process of querying both a collection and a subcollection in Firebase using AngularFire?

I have a structure in my firebase database that looks like this: /profiles/{uid}/displayName /email /otherAttribues /roles/{roleName}/someAttribute /someOtherAttribute The reason ...

Tips for distinguishing the beginning and ending points of wrapped text in the Firefox browser

Within my work, I am utilizing a contentEditable span where I aim to position an element with position: absolute on the same line as the cursor. However, issues arise when text wrapping occurs - causing abnormal behavior at the start and end of wrapped lin ...

Automate scrolling to the top of a webpage with Selenium WebDriver using JavaScript

I am utilizing Selenium webdriver with javascript and node.js During a certain step in my test, I need to automate clicking on a button positioned at the top of the page. However, due to some pre-treatment, the page automatically scrolls to the bottom, c ...

Wheelnav.js implementing a dynamic bouncing animation

I'm currently in the process of working on a pie menu with wheelnav.js and everything is going smoothly so far. However, I can't seem to find any information in the wheelnav.js documentation on how to eliminate the bouncing effect when a menu cho ...

A guide on declaring global variables with TypeScript validation in a JavaScript file

Currently, I am utilizing TypeScript for type checking of JS files. This involves adding a // @ts-check comment at the beginning of the file. However, I am encountering an issue with a global variable that has been assigned separately using window. // inde ...

What mechanism enables the scores on this sports ticker to refresh automatically without relying on ajax calls?

While browsing scores on , I found myself intrigued by the way they update their scores without using ajax calls or iframes. It's a mystery to me how this functionality is achieved. Can anyone shed some light on how it works? ...