JavaScript-driven bootstrap 4 collapsing functionality

I have created a Bootstrap 4 page with collapsible accordion tables. Each table has a fa-plus-circle icon that changes to a fa-minus-circle icon when clicked. However, I am unable to change it back to the plus-circle icon when clicked again. Can someone please assist me with this issue?

<link rel="stylesheet" href="../../assets/css/bs/bootstrap.css">
      <link rel="stylesheet" href="../../assets/css/custom/custom.css">
      <link rel="stylesheet" href="../../assets/css/bs/bootstrap.min.css">
      <link rel="shortcut icon" href="../../assets/img/logo/logoCircle.png" type="image/x-icon">
      <link rel="stylesheet" href="../../assets/css/fontawesome/all.css">
      <link rel="stylesheet" href="../../assets/css/animate/animate.css">
 <script type="text/javascript">
                 toggleCircle = function(e) {
                  if ($(e.children).attr("class").includes('down')) {
                    $(e.children).removeClass("fa-minus-circle");
                    $(e.children).addClass("fa-plus-circle");
                  } else {
                    $(e.children).removeClass("fa-plus-circle");
                    $(e.children).addClass("fa-minus-circle");
                  }

                }
                      </script>
    <div class="container">
     <div class="row">
    <div class="accordion col-12" id="accordionExample">
        <div class="card">
            <div class="card-header bg-light" id="headingOne">
              <h2 class="mb-0">
        <button class="btn  text-dark collapsed" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="false" aria-controls="collapseOne" onclick="toggleCircle(this)" >
          <span class="fa fa-plus-circle"></span>
          &nbsp;&nbsp;
          How do I recover my account? &nbsp;
        </button>
              </h2>
            </div>

            <div id="collapseOne" class="collapse" aria-labelledby="headingOne" data-parent="#accordionExample">
              <div class="card-body">
                <span>Were sorry to hear that! If you can't sign in to your Pandayan EGC Account.<br><br>

                            To reset your password:<br>
                              1. Go to the login page and you see the forgot your password and click that. Enter your email address click proceed.<br>
                              2. Once you click the proceed button go to your gmail account and there's an email. <br>
                              3. After that you create new password and click save changes. <br>  
                 </span>
              </div>
            </div>
          </div>
        </div>
</div>
</div>
<script src="../../assets/js/jquery/jquery-3.3.1.js"></script>
      <script src="../../assets/js/popper/popper.min.js"></script>
      <script src="../../assets/js/bs/bootstrap.min.js"></script>
      <script src="../../assets/js/fontawesome/all.js"></script>
      <script src="../../assets/js/backstretch/backstretch.js"></script>
      <script src="../../assets/js/backstretch/backstretchSettings01.js"></script>
      <script src="../../assets/js/bsn/bootstrap-notify.min.js"></script>
      <script src="../../assets/js/general/loader.js"></script>
      <script src="../../assets/js/general/login.js"></script>
      <script src="../../assets/js/general/forgotpassword.js"></script>
      <script src="../../assets/js/general/accountregister.js"></script>

Answer №1

Feeling the urge to emphasize that you do not require any JS at all! Just a single line of CSS

.collapsed .fas:before {
  content: "\f055";    /* https://fontawesome.com/cheatsheet */
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">

<button class="btn  text-dark collapsed" type="button " data-toggle="collapse" data-target="#collapseOne" aria-expanded="false" aria-controls="collapseOne">
  <span class="fas fa-minus-circle"></span> How do I recover my account?
</button>

<div id="collapseOne" class="collapse">OU YEAH</div>


<script src="https://code.jquery.com/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

Additionally, it makes sense to utilize

<span class="fas fa-minus-circle"></span>
if you begin in a collapsed state.


In essence, Bootstrap already dynamically toggles the collapsed button class. Therefore, all the aforementioned code does is pinpoint that class using CSS, locate the .fas icon, and switch it to the required one.

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

Expanding VS 2013: Effective management of classes through item customization

Trying to accomplish a somewhat complex task, I have dedicated hours to searching for the appropriate documentation with no success. The goal is for Visual Studio to generate some JavaScript code and place it in a specific folder, starting from: Performi ...

Is the next method in the .then chain called only after the completion of setState?

I'm currently facing an issue with the execution order of methods in my React app: The problem is that the createQuestions() function is running before the findEmployeeId() method is completed. I believed that the .then should ensure it waits for the ...

How to Create a Responsive Layout with Bootstrap 5: Cards, Grids, and Columns

Struggling to create an HTML layout that resembles the image provided. Despite using Bootstrap 5, I'm having trouble achieving the desired responsiveness. Any assistance or guidance would be greatly appreciated! https://i.sstatic.net/W1Sh0EwX.jpg I ...

Discover how to retrieve an array of objects by utilizing the Object.keys() and map() functions in JavaScript

How can I iterate through an array of objects with a specific structure? My goal is to construct a tree representation of the data, starting with the languages object in each entry and recursively accessing its nested objects using Object.keys() and map(). ...

What is causing the newly generated input tags to disappear from the page?

I'm having an issue where my code successfully creates new input tags based on user input, but they disappear shortly after being generated. What could be causing this to happen? <form> <input type='text' id='input' /> ...

Error message: The Slick Carousal encountered an unexpected problem - TypeError:undefined is not a function

I'm having an issue with a script for a Slick Carousel inside of some Ajax Tabs. I keep encountering the Uncaught TypeError: undefined is not a function error, but I'm unsure what exactly it's pointing to. $(document).ready(function(){ ...

Enhance Your Website with jQuery Loading Using Bootstrap's Less Features

Hey everyone! So I had a question a few days back that I couldn't find the answer to: why was the content appearing weird and not formatted correctly when I used jQuery's .load() function to load another .HTML page from my directory? The strange ...

Interactive search functionality using jQuery

In the panel I have, I would like to include a search input field that allows users to type in a word. As soon as a match is found, that specific word will be highlighted within the panel while the other information disappears. ...

Each time the toggle is switched, an additional AJAX request is sent to the PHP document

I've been working on a function that sends data to a PHP file, which then processes the data. The PHP part works fine, but I'm having issues with the jQuery side of things. Each time I click on a ".work" element and trigger the toggle function, a ...

Insert HTML elements using javascript

Is it possible to change this JavaScript code in the following way: divElement.appendChild(document.createTextNode("<h7>Optie " + inputNumber + ": </h7>")); What would be the best approach to achieve this? ...

Encountering an error when implementing a router object within a TypeScript class in a Node.js environment

I have a Node.js service written in TypeScript. I am currently working on implementing a separate routing layer within the application. In my app.js file, I have the following code: let IndividualRoute= require('./routing/IndividualRoute'); app ...

Utilizing JSON strings within an onclick function

Hey there, currently I am working on sending an encoded JSON through the onclick attribute. However, I am facing a challenge because the JSON contains strings with a lot of apostrophes and quotes which end up closing the quotes in the onclick attribute. Up ...

How can the name of an element be passed as an argument to a function called by that element within a Vue component?

I'm currently developing an interactive SVG map inspired by the one found on the CDC page. In my SVG map component, I have implemented multiple path elements that each contain a unique name attribute representing the corresponding state. Additionally, ...

Retrieving the value of a submit button within the `onSubmit` event handler in a React application

In my usual process, I typically handle the form submission by utilizing the onSubmit handler. <form onSubmit={e => { ... } }> <input ... /> <input ... /> <button type="submit">Save</button> </form> ...

Having trouble sending values via POST request in Node.js using Express

Currently, I am in the process of learning how to use Express (v4) with Node.js. My main goal right now is to create a basic REST API. This API specifically focuses on one endpoint: /orders. The main functionality I am trying to achieve is the ability to r ...

Leveraging Material-UI: Utilize props in useStyles method while employing Array.map()

After delving into the world of passing props to makeStyles in Material-UI, I stumbled upon this insightful answer. The solution presented involves passing props as a variable, which is quite useful. However, my aspiration is to extend this functionality t ...

Error: The object 'window' is not recognized - Issue with Next.js slider

I'm having trouble debugging a reference error issue on Windows. ReferenceError: window is not defined at /home/ubuntu/Desktop/project/my-app/node_modules/@splidejs/splide/dist/js/splide.js:5857:1 at Object.<anonymous> (/home/ubuntu/Desk ...

Adding persistent active classes to navigation elements within the navbar context using AngularJS

I am working on a navigation list element in AngularJS and I want to store the active tab so that it adds the 'active' class whenever there is a reroute. Currently, my goal is to simply log the name of the active tab. Since I have never linked a ...

Preserving the position of inline text

I've been working on a button design that I want to make look more realistic by moving the text down 1px inside the button when it's pressed. To achieve this effect, I added an extra pixel to the top and removed one from the bottom. However, the ...

Executing an ajax request following validation

I am facing an issue with my ajax insert script and validation script. Currently, the insert script is executing regardless of the validation result. How can I modify my code to ensure that the insert query only runs after successful validation? Below is ...