0

I have a simple accordian I have made with Jquery. It toggles open and close on the item and also closes down any other active items.

Here is the code:

  $(document).ready(function($) {
    $('#accordion').find('.accordion-toggle').click(function(){

    $(this).toggleClass('activeState');

      // Remove active state from other panels not selected
      $("#accordian").not($(this)).removeClass('activeState');

      //Expand or collapse this panel
      $(this).next().slideToggle('fast');

      //Hide the other panels
      $(".accordion-content").not($(this).next()).slideUp('fast');

    });
  });

Right now I am just using the 'activeState' class to add a background colour of yellow (later I am looking to use icon classes). Now it toggles well when opening and closing the same item. But I want it to toggle/remove the activeState class when another accordian item is clicked on (then obviously append the activeState class to the newly opened accordian item. I am attempting to use the ".not()" jQuery function to remove the activeState class from any of the '.accordian-toggle' items that is not the active object being clicked on. I am here because I am not having any luck with it.

Here is the JSFiddle https://jsfiddle.net/0LLncd4d/26/ Can I please ask for some assistance in what I may have overlooked?

1
  • 2
    Try using this for unselecting $(".accordion-toggle").not($(this)).removeClass('activeState'); Commented Feb 22, 2016 at 6:49

2 Answers 2

3

replace the two lines

$(this).toggleClass('activeState');
$("#accordian").not($(this)).removeClass('activeState');

with

$('.accordion-toggle').removeClass('activeState');
if($(this).parent().find('.accordion-content').css('display')=='none'){
   $(this).addClass('activeState');
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help. I already tried this before and whilst it works to toggle off the active state when clicking on another item - it fails to remove it when you just keep toggling the one item. jsfiddle.net/0LLncd4d/28
Actually like what yours can do when I use :after with CSS content property jsfiddle.net/0LLncd4d/30
1

Try like this:

 $(document).ready(function($) {
$('#accordion').find('.accordion-toggle').click(function(){

$(this).toggleClass('activeState');

  // Remove active state from other panels not selected
  $(".accordion-toggle").not($(this)).removeClass('activeState');

  //Expand or collapse this panel
  $(this).next().slideToggle('fast');

  //Hide the other panels
  $(".accordion-content").not($(this).next()).slideUp('fast');

  });
 });

1 Comment

I see. Simple fix. Replace $("#accordian").not($(this)).removeClass('activeState'); WITH $(".accordion-toggle").not($(this)).removeClass('activeState'); THANKS!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.