Rotating arrows enhance the functionality of the accordion menu

I have successfully implemented a basic accordion with rotating arrows on click. Everything is working smoothly except for one issue:

  • When I collapse one item and then try to collapse another, the previous arrow does not return to its default state.

Is there a way to ensure that the arrow returns to its default state when clicking on a different collapsible item?

$(function() {
  $('.arrow-r').on('click', function() {
    $(this).find('.fa-angle-down').toggleClass('rotate-element');
  })
})
.rotate-element {
  @include transform (rotate(180deg));
}
.fa-angle-down {
  &.rotate-icon {
    position: absolute;
    right: 0;
    top: 17px;
    @include transition (all 150ms ease-in 0s);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="collapsible collapsible-accordion">
  <li><a class="collapsible-header arrow-r"> Menu 1<i class="fa fa-angle-down rotate-icon"></i></a>
    <div class="collapsible-body">
      <ul>
        <li><a>Item 1</a>
        </li>
        <li><a>Item 2</a>
        </li>
      </ul>
    </div>
  </li>
  <li><a class="collapsible-header arrow-r"> Menu 2<i class="fa fa-angle-down rotate-icon"></i></a>
    <div class="collapsible-body">
      <ul>
        <li><a>Item 1</a>
        </li>
        <li><a>Item 2</a>
        </li>
      </ul>
    </div>
  </li>
  <li><a class="collapsible-header"> Menu 3</a>
    <div class="collapsible-body">
      <ul>
        <li><a>Item 1</a>
        </li>
        <li><a>Item 2</a>
        </li>
      </ul>
    </div>
  </li>

Answer №1

Here is a potential solution:

$(document).ready(function() {
  $('.arrow-right').on('click', function() {
    //Reset all other arrows
    $('.arrow-right').not(this).find('.fa-angle-down').removeClass('rotate-element');
    //Rotate or reset the clicked arrow
    $(this).find('.fa-angle-down').toggleClass('rotate-element');
  });
});

Answer №2

In case anyone else is struggling, here's the solution that worked for me: I made a slight adjustment by including ".arrow-r" after the first .find method.

I had 4 main Accordions with multiple inner Accordions, but I only wanted the main arrow on each main accordion to animate. This tweak prevented the inner accordions from triggering the main animation.

$(function() {
     $('.arrow-r').on('click', function() {
        //Reset all except the current one
        $('.arrow-r').not(this).find('.arrow-r').removeClass('rotate-element');
        //Rotate or reset the clicked one
        $(this).find('.fa-angle-down').toggleClass('rotate-element');
      })
    })

If you need more context, here's the code snippet:

$(function() {
  $('.accroordlite').on('click', function() {
    //Reset all except the current one
    $('.accroordlite').not(this).find('.accroordlite').removeClass('down');
    //Rotate or reset the clicked one
    $(this).find('.spinOnClick').toggleClass('down');
  })
})

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

When trying to access a view through a controller, the CSS is failing to apply properly

I'm having issues with CSS not working in my Codeigniter project, even when using the base_url() function. < link href="< ? php echo base_url(); ?> application/views/vehicle/css/bootstrap.min.css" rel="stylesheet" media="screen" /> ...

I am currently attempting to extract data from a JSON file by using key names for reference, but I am running into issues when dealing with nested keys

Is there a better way to retrieve values from a JSON file by matching key names? The current method I am using does not seem to work with nested keys, so any suggestions on alternative approaches would be appreciated. // Sample .JSON file { "ro ...

Connecting factors

Let me simplify what my code does: var list = { "group":{ "subgroup1":[{"name1":"Jimmy","name2":"Bob"}], "subgroup2":[{"name1":"Sarah","name2":"Nick"},{"name1":"Kevin","name2":"George"}] } } function group(group,name){ var link ...

What is the process for building a JSON-formatted dictionary?

My intention is to structure my data in a way that can be accessed using a specific key. The current arrangement looks like this: const dict = []; dict.push({"student": "Brian", "id":"01", "grade":& ...

Ways to access the content within a div tag

Is there a way to retrieve the ID of the textarea when clicking on the image within the anchor and div tags? Here is the relevant HTML code: <img src="Image/icons/preview.png" alt="Preview" id="img1" onclick="ViewHtml(this.id);" /> <a id="ancH ...

Make sure that the input field and label are aligned side by side in the

Is there a way to arrange the following HTML in the specified layout? <div> <div> <label>Counterparty</label> <input id="paymentsApp-inpt-cpty" ng-model="selectedPaymentCopy.counterparty" ng-required="true" ...

Flexbox - managing wrapping on legacy devices

Utilizing flexbox, I have successfully designed a 2 column layout with a div at the top spanning both columns for a menu format. While this works flawlessly in Chrome and iOS7, it appears to be ineffective in outdated versions of Safari. In this fiddle, yo ...

Rotate a shape to form a Rhombus at the center

I used the transform CSS property to create a rhombus, but I noticed that the center point of the shape is off-center to the right rather than in the middle. Can anyone help me figure out how to fix this issue? You can view my code here. .rhombus{ width ...

Monitor a webhook on the server side with Meteor

I have created a meteor application using iron-router. I want the app to be able to receive webhooks from another service, essentially creating an API for other services to utilize. For example, when an external website calls myapp.meteor.com/webhook, I n ...

Fetching data from ExpressionEngine using AJAX

Looking to display ExpressionEngine entries in a popup modal using jQuery and ajax. When the user clicks on an entry title, the full entry should be displayed in the modal. Additionally, next and previous buttons within the modal are desired for navigation ...

What is the best method for modifying an XML document without altering its associated XSL stylesheet?

I am working with XML data retrieved from a URL: <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="/style.xsl"?> ....etc... To display the data in HTML format, I added the second line referencing the style.xsl file ...

The filtering function stops working after the initial use

As I develop an app using React and Redux, my goal for the following code snippet is to function as a reducer within the main application. I have imported a filterData function, which works seamlessly the first time any Action type is selected. However, it ...

Pause file uploads, display modal popup, then resume uploading

Is there a way to pause file uploading in order to display file requirements before proceeding? For example, when a user clicks on "upload file", I would like to show them a modal window with details about the file they need to upload. Once they click ok, ...

What is the process for viewing the collections of all databases while logged in as an administrator?

My aim is to display all databases along with their collections, similar to logging in as an admin user into robo3T, for example. Using the two commands below poses no issue. However, when I use the listCollections command, I only get the collections from ...

Retrieving data via AJAX from an SD card

I am using the Atmel SAM4E-EK microcontroller as a server to host a webpage and send data over HTTP protocol. I am currently facing an issue with the download function while trying to download a file from my sd_card, which will not be larger than 512MB. s ...

Is it possible to utilize the spread operator for combining arrays?

Consider two different arrays represented by variables a and b. The variable c represents the output as a single array, indicating that this method combines two or more arrays into one. let a=[a ,b]; let b=[c ,d]; c=[a,...b] The resulting array will be: ...

Utilizing environment variables in a Rails-AngularJS (1.5) project

Currently working on a Rails-AngularJS (1.5) project and encountering difficulties accessing my ENV variables in the JavaScript components such as services and console. I've implemented dotenv, stored my SECRET_KEY in a .env file, and included the fo ...

Assistance Required for Troubleshooting Error in Codeigniter Ajax

Hey there, I'm a newcomer to CI and I've been searching the web for a tutorial that actually works, but so far no luck. Can someone please assist me with this code: How can I modify the code to allow for submitting an entry to the database via a ...

Error: The sort method cannot be applied to oResults as it is not a

I encountered this issue. $.ajax({ url: '${searchPatientFileURL}', data: data, success: function(oResults) { console.log("Results:...->"+oResults); oResults.sort(function ...

Employing promises for fetching data via XHR results in a promise that is still pending

Currently, I am experimenting with promises to handle asynchronous requests using XHR. Interestingly, I noticed that when I try to log the result within the .then function, it works perfectly fine. However, if I attempt to log it outside of this scope, it ...