Accordion menu side icon modifications when expanding

This is a menu with a collapsing feature. I would like to change the right side icon when expanding the menu.

In order to achieve this, I need to add

<i class="fa fa-caret-down pull-right"></i>
in place of the current fa-caret-down icon and switch it to fa-caret-up upon clicking.

.category-list {
  width="100px";
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.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="container">
<div class="row">
<div class="col-xs-3">  
<div class="category-list" id="category-list">
  <div class="list-name" data-toggle="collapse" data-target="#all">All
    <i class="fa fa-caret-down pull-right"></i>
  </div>

  <ul id="all" class="collapse">
    <li><a href="">Test1</a>
    </li>
    <li><a href="">Test2</a>
    </li>
    <li><a href="">Test3</a>
    </li>
  </ul>

<div class="list-name" data-toggle="collapse" data-target="#nxt">Next
    <i class="fa fa-caret-down pull-right"></i>
  </div>

  <ul id="nxt" class="collapse">
    <li><a href="">Test1</a>
    </li>
    <li><a href="">Test2</a>
    </li>
    <li><a href="">Test3</a>
    </li>
  </ul>



</div>
  </div>
</div>
  </div>

Answer №1

Include the .fa-caret-up element and set up the necessary CSS rules to display or hide the caret icons depending on the state of the .list-name element's aria-expanded attribute:

  1. For aria-expanded="true": Only the .fa-caret-up icon will be shown
  2. For aria-expanded="false": Only the .fa-caret-down icon will be visible

.category-list {
  width="100px";
}

.list-name:not([aria-expanded="true"]) .fa.fa-caret-up,
.list-name[aria-expanded="false"] .fa.fa-caret-up,
.list-name[aria-expanded="true"] .fa.fa-caret-down {
   display:none;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.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="container">
<div class="row">
<div class="col-xs-3">  
<div class="category-list" id="category-list">
  <div class="list-name" data-toggle="collapse" data-target="#all">All
    <i class="fa fa-caret-down pull-right"></i>
    <i class="fa fa-caret-up pull-right"></i>
  </div>

  <ul id="all" class="collapse">
    <li><a href="">Test1</a>
    </li>
    <li><a href="">Test2</a>
    </li>
    <li><a href="">Test3</a>
    </li>
    <li><a href="">Test4</a>
    </li>
    <li><a href="">Test5</a>
    </li>
    <li><a href="">Test6</a>
    </li>
    <li><a href="">Test7</a>
    </li>
    <li><a href="">Test8</a>
    </li>
  </ul>


</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 techniques does Twitter use to insert labels into text boxes?

When I visited the Twitter login page, I noticed something interesting - the labels for input fields were inside the input fields themselves. It seemed to be accomplished using a common trick involving javascript/jquery. Intrigued, I delved into the Twitte ...

How to retrieve the chosen option from dropdown list using jQuery dialog

Having some trouble with a jQuery dialog in ASP.NET. I'm attempting to retrieve the selected value from a dropdownlist that is displayed within the jQuery dialog. The process involves the user clicking a button to open the dialog, selecting an item fr ...

"Enhancing user experience: Rotating hamburger menu icons with a simple click

I'm facing an issue with my hamburger menu, specifically with the icons. Here's how my HTML is structured: <input type="checkbox" id="check"/> <header class="IndexHeader"> <nav class="navigat ...

The function of the 'Class' attribute is not functioning properly

I am currently working on a search field with a drop-down menu that determines the type of data being searched. This, in turn, dictates the input type for the search field. For example, if I am searching by "name" or "ID number", the input type is set to t ...

Having trouble with radio button validation in JS?

I'm having difficulty understanding why the radio button is not staying checked when I select another option. To demonstrate, I created a fiddle where initially the "diy" button is selected but switching to "paid" causes the radio button to jump back ...

Having trouble with the jQuery select2 AJAX functionality?

I've been experimenting with the jQuery select2 plugin and attempting to integrate AJAX with my external data, but unfortunately it's not working as expected. I'm curious if anyone can help me identify what mistake I might be making or if th ...

Keep checking the checkbox until it has been clicked

I am working on creating a checkbox to verify if the user is human, but I've encountered an issue (Code provided below). My goal is for the checkbox to remain checked once it has been clicked. I attempted using onclick but it doesn't seem to be e ...

Tips for enhancing the display of tables on mobile devices?

I am currently utilizing for creating a table This is the HTML code I have implemented: <table class="responsive-table"> <thead> <tr> <th>Mon</th> <th>Tue</th> <th>Wed</th ...

Building a row of five dynamic columns using Bootstrap's responsive design

I'm looking to set up a row with 5 responsive columns that have equal padding on all sides, but I'm struggling to find the best way to do this using Bootstrap. Here's my current HTML: <div class="row five-col-row"> < ...

Positioning images in sync with the text of captions

I am trying to arrange multiple images in a row with captions underneath each of them. I want the images aligned while allowing the captions to flow naturally below the images, even if they vary in length. Despite following advice from previous posts on SO ...

How can I identify when a browser window is maximized using JavaScript or CSS?

I am currently working on a dashboard designed for static display on large monitors or TVs for clients. My main goal is to implement CSS styling, specifically a snap-scroll feature, but only when the display is in 'fullscreen' or 'maximized& ...

What is the correct method for integrating jQuery libraries into an Angular project version 10 or higher?

Currently, I am facing difficulties while attempting to install the jquery and jquery-slimscroll packages into an Angular project (version greater than 10). It appears that the installation of these packages has not been successful. In light of this issue, ...

Combining CSS sprite sheets with optimized HTTP requests makes for faster loading

Quick query about CSS: I have a sprite sheet that I'm trying to utilize from two different classes. The CSS code I am using is as follows: .buttons, .expand:before { background-image: url('images/spriteSheet2.png'); } Later on, I adjus ...

Implementing Jquery Table Selection with Custom CSS Class

When I attempted to use the selectable feature on a table with a CSS class applied, the selection did not work. However, when I removed the CSS class, the selection worked perfectly fine. I suspect that the issue lies within the CSS definition, but I' ...

The jQuery text input field is unable to be hidden using the hide

My table looks like this. <input type=checkbox onclick=enableEdit(this)/> <tr> <td onclick=enableInput(this)> <input style="display:none"> <span>text1</span> </td> <td onclick=enableInput(this)> ...

1 out of the 3 Submit buttons in the form is malfunctioning and not working properly

Scenario : There is a Bootstrap 4 form with: 1 submit button at the bottom. 2 modals, each with a submit button. Working Submit buttons : The main submit button is functional. The submit button for Add new contractor modal is also working. ...

What are the functionalities of display flex and block?

I am having trouble with my CSS setup. I have an outer container that wraps around an inner container, which in turn contains two divs. The inner container has a display property of flex, but for small screen sizes, I want the two divs to stack up and disp ...

What is the method that Google uses to automatically load the footer at the bottom of the web page?

Google manages to keep its footer at the bottom of the browser regardless of resolution when fully maximized. How can I achieve a similar effect? ...

How can I create a basic jQuery validation that only applies to the border and background without affecting the text?

I have been on a lengthy quest to find the simplest way to implement jQuery validation - essentially changing border color and background. No frills, just the basics! Many methods I've come across seem to involve a lot of code. Take this snippet as a ...

When an app is sent to the background and then resumed, the UIWebView containing an HTML5 database fails to load

I've encountered a peculiar issue with an app featuring a primary view that includes a UIWebView. The app has a login view controller (built with UIKit) embedded in a navigation controller. Upon successful login, the main view controller is pushed, sh ...