What is causing jQuery toggleClass to fail in removing a class?

I have successfully implemented a Skills accordion feature, but I am now trying to add a button that toggles all the skill sections at once. However, I am facing issues with jQuery not correctly adding or removing classes on the .accordion-trigger-all element. Initially, I used .click() method, but then switched to .on("click", function) without any success.

$(document).ready(function() {

  $(".accordion .accordion-trigger.accordion-trigger-single:not(.open) + .skill-container").hide()

  $(".accordion .accordion-trigger.accordion-trigger-single").click(function() {

    $(this).next(".skill-container").slideToggle()

    $(this).toggleClass("open")

    toggleAll()

  })

  function toggleAll() {
    if ($(".skill-container").is(":hidden")) {

      $(".accordion .accordion-trigger.accordion-trigger-all h3").html("Open All")
      $(".accordion .accordion-trigger.accordion-trigger-all").removeClass("open")

    } else {

      $(".accordion .accordion-trigger.accordion-trigger-all h3").html("Close All")
      $(".accordion .accordion-trigger.accordion-trigger-all").addClass("open")

    }
  }

  toggleAll()
  //
  $(".accordion .accordion-trigger.accordion-trigger-all:not(.open)").on('click', function() {

    $(".accordion .accordion-trigger.accordion-trigger-single:not(.open) + .skill-container").slideDown()
    $(".accordion .accordion-trigger.accordion-trigger-single:not(.open)").toggleClass("open")

    $(".accordion .accordion-trigger.accordion-trigger-all.open h3").html("Close All")
    $(".accordion .accordion-trigger.accordion-trigger-all").addClass("open")

    toggleAll()

  })

  $(".accordion .accordion-trigger-all.open").on("click", function() {

    $(".accordion .accordion-trigger-single.open + .skill-container").slideUp()
    $(".accordion .accordion-trigger-single.open").toggleClass("open")

    $(".accordion .accordion-trigger.accordion-trigger-all h3").html("Open All")
    $(".accordion .accordion-trigger.accordion-trigger-all").removeClass("open")

  })

})
.accordion .accordion-trigger {
  cursor: pointer;
  text-align: left;
  width: 100%;
  border: none;
  color: #9CA3AF;
  border-bottom: 3px solid #9CA3AF;
  background-color: #F9FAFB;
}

.accordion .accordion-trigger:not(:first-of-type) {
  margin-top: 30px;
}

.accordion .accordion-trigger.open {
  border-bottom: #059669 3px solid;
  background-color: #ECFDF5;
}

.accordion .accordion-trigger h3 {
  padding: 6px;
  font-weight: initial;
  margin: 0;
  color: #9CA3AF;
}

.accordion .accordion-trigger.open h3 {
  color: #059669;
}

.accordion .skill-container * {
  box-sizing: border-box;
}

.accordion .skill {
  display: inline-block;
  text-align: left;
  color: white;
  padding: 10px 0 10px 4px;
  border-radius: 5px;
  background-color: lightgray;
  white-space: nowrap;
  margin: 10px 0;
  overflow: hidden;
  position: relative;
  z-index: 0;
  width: 100%;
}

.accordion .skill::before {
  content: "";
  position: absolute;
  background-color: #312E81;
  border-radius: inherit;
  z-index: -1;
  top: 0;
  bottom: 0;
  left: 0;
  animation: animateLeft 1s ease-out;
  width: var(--l);
}

.accordion .skill:nth-child(even)::before {
  background-color: #374151;
}

@keyframes animateLeft {
  from {
    left: -100%
  }
  to {
    left: 0
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

...
  
</div>

Answer №1

After some investigation, I have come to a solution for this issue: When jQuery is loaded, it assigns an event handler to the

$(".accordion .accordion-trigger-all.open").on('click', function()
, which means it only detects elements that are open initially. However, if the class is removed and the search is conducted again, the element is not found.

The fix is quite simple:

$(".accordion").on('click','.accordion-trigger-all.open', function()

This will attach the event handler to the entire element rather than just those with the class .open

The same approach should be taken for the opposite scenario:

$(".accordion").on('click','.accordion-trigger-all:not(.open)', function()

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

JavaScript plugin designed for effortless conversion of JSON data to structured HTML code

I want to add this JSON data to an HTML element. [ { "website":"google", "link":"http://google.com" }, { "website":"facebook", "link":"http://fb.com" } ] Is there an easy way to convert this using a plugin ...

The React application is being continuously accessed by a node.js server and processed within async functions

Currently, I am utilizing React, MongoDB, Node.js, and Express in my project. The specific scenario I am facing involves the following code snippet within my component: renderWishlist(){ var quantity; var itemID; var tmp; var my ...

What is the method for adding an event listener in AngularJS within an ng-repeat iteration?

I'm completely new to AngularJS and currently working on building a photo gallery. The basic idea is to have an initial page displaying thumbnail photos from Twitter and Instagram using an AngularJS ng-repeat loop. When a user hovers over an image, I ...

Bootstrap for Twitter: How to Customize Text in the Navigation Bar

As per the twitter bootstrap guidelines, I am supposed to enclose text in a <p> tag for correct leading and color. However, when I try to do this within the navbar element or any of its sub-levels, the text does not inherit any of the styling propert ...

Align a header and a block of text in a vertical manner within a div

Struggling to align a h1 element and p vertically in the middle of a left-floated div, but they end up next to each other aligned vertically. Seems like table-cell display is the culprit, but not sure how to achieve vertical alignment without it. The curr ...

Tips for binding data to numerous dynamic controls

Implementing reactive forms in my Angular project allowed me to create a simple form for adding employee work hours and breaks. The challenge I encountered was the inability to bind data from break controls. In the .ts file export class AddAppointmentForm ...

The Bootstrap modal fails to appear when closed

After opening and closing the modal window, clicking on the button does not display the modal window again. The screen remains grayed out and the content of the modal window does not appear. yyyyyyyyyyyyyyyy function customShortcode() { ob_start(); ...

Trouble with CSS styling arising when launching the MVC application on IIS

Currently working on a website in ASP.Net MVC 5. I am trying to test it on my local IIS (version 8), but encountering an issue where the CSS styling is not being applied. In my _layout.cshtml file, I opted for direct link tags instead of using bundles. &l ...

I have noticed that the baseline of a Span element has shifted after updating my Chrome browser to a version that begins with

Once I updated to chrome Version 108.0.5359.94 (Official Build) (64-bit) from 107.0.5304.87 (Official Build) (64-bit), the behavior of the span element changed drastically. It shifted its baseline when multiple spans were stacked on top of each other. Exp ...

After the page has finished loading, I aim to have the modal window pop up after 10 seconds. Thank you to all!

$(document).ready(function() { var id = "#dialog"; //Calculate screen dimensions var maskHeight = $(document).height(); var maskWidth = $(window).width(); //Adjust mask to cover entire screen $('#mask').css({'wid ...

Efficiently repositioning Kendo Mobile Buttongroup to a new row as needed

In my current project, there is a specific requirement to display three button groups in a row. If there are more than three buttons, they should move to the next row dynamically as the data will be fetched from the server. For reference, below is a sampl ...

Creating a customizable Sass Mixin for animation keyframes that incorporates various stages and the transform property

My goal is to generate the standard CSS using a SASS Mixin for efficiency. STANDARD CSS @-webkit-keyframes crank-up { 100% { -webkit-transform: rotate(360deg);} } @-moz-keyframes crank-up { 100% { -moz-transform: rotate(360deg);} } @-o-keyframes cran ...

Guide to Inputting Numbers in a Form Field using a Pop-up Keypad (with Javascript/AJAX)

I am working on a small project that involves creating a keypad with buttons for all the digits, backspace, and decimal. When these buttons are clicked, they should populate a form field like a text box. The keypad will be located next to the form field as ...

Assign a value to a variable based on the button that was clicked using a form

My form consists of 8 buttons, each representing a different color: .settings-theme-buttons{ height: 4vw; width: 4vw; border: none; display: inline-block; border-radius: 100%; cursor: pointer; } #settings-theme-white{ b ...

The background-size is set to cover, but it does not completely cover the

New to HTML here, I'm attempting to utilize background-size: cover; to ensure an image covers the entire screen, but I am still noticing a gap. The issue may be related to this section: body { background-image: url("hexagon.gif"); ...

Retrieve various SVG file contents with a JavaScript/AJAX function

I am facing an issue with loading multiple SVG files asynchronously. I have created a function to accomplish this: function loadSVG(fileName){ var getSVG = new XMLHttpRequest(); getSVG.open('GET','assets/svg/'+fileName+'.svg&a ...

Why is it that a label tag cannot be placed directly above an input tag?

On this particular page: http://getbootstrap.com/components/#input-groups-buttons If you modify the Go! button to a label (using Chrome inspector), you will observe that the Go! button is no longer positioned on top of the input field: https://i.sstatic ...

Utilize IntelliJ's TypeScript/JavaScript feature to extract a method from all instances

I am relatively new to using IntelliJ Idea Ultimate 2020 and I am currently exploring the refactoring functionalities within the software. Is there a way to extract a method from a section of code and apply it to all instances easily and exclusively withi ...

Upgrading the Spring framework from version 3.0.2 to 3.2.0 can lead to issues with Spring JSON functionality

While working on a web application using the Spring framework version 3.0.2 and Hibernate in NetBeans 6.9.1, I encountered a bug related to uploading multiple files as discussed in a previous question here. Despite my efforts, I couldn't find a soluti ...

Choose a different option when there is a change

Here is an example of JSON data: [{ "user_id": "113", "employe_first_name": "Asaladauangkitamakan", "employe_last_name": "Nasibb" }, { "user_id": "105", "employe_first_name": "Ryan", "employe_last_name": ...