Try implementing toggleClass() in the accordion feature rather than addClass() and removeClass()

Hey there! I've implemented accordion functionality using the addClass() and removeClass() methods. Here's a breakdown of what I did:

<div class="container">
    <div class="functionality">Accordion</div>
    <ul class="accordion-wrapper">
        <li>
            <div class="title">Title</div>
            <div class="text">Description</div>
        </li>
        <li>
            <div class="title">Title</div>
            <div class="text">Description</div>
        </li>
        <li>
            <div class="title">Title</div>
            <div class="text">Description</div>
        </li>
    </ul>
</div>

I styled it with CSS like this:

ul {
    list-style: none;
}

.accordion-wrapper .title {
    border: 1px solid #0F0F0F;
    background: #CCCCCC;
    font-size: 14px;
    margin: 0 10px 0 10px;
    padding: 6px 8px;
    cursor: pointer;
}

.accordion-wrapper .text {
    display: none;
    font-size: 12px;
    padding: 8px 16px;
    text-align: justify;
}

.accordion-wrapper .active {
    display: block;
}

.functionality {
    font-size: 16px;
    margin: 12px 8px;
    padding: 6px 8px;
}

And here's the JavaScript part:

(function($) {
    $(function() {
        function accordion() {
            var $clicked = $('.title');
            var $text = $('.text');

            $clicked.click(function() {
                var $instance = $(this);
                var $currentText = $instance.closest('li').find('.text');
                if ($currentText.hasClass('active')) {
                    $currentText.removeClass('active');
                } else {
                    $text.removeClass('active');
                    $currentText.addClass('active');
                }
            });
        }
        accordion();
    });

})(jQuery);

Now, I'm looking for a way to switch from using addClass() and removeClass() to toggleClass(). Any ideas or suggestions on how I can do that without creating a new accordion? Thanks!

Answer №1

To simplify your code, swap out the if.. else block with .toggleClass(). Don't forget to include $text.removeClass('active'); to eliminate any existing active classes on the .text elements.

Further details are provided in the code comment below.

/* Your JS has been refined */

(function accordion($) {
  // Avoid storing variables since they are used only once

  $('.title').on('click', function() {
    // Instead of closest and find, a simple .next() suffices here
    var $currentText = $(this).next();
    
     // Replace your if.. else with the code below
                
     /* Add this to remove all previously added active classes, you can skip this if multiple dropdowns should stay active */
       $('.text').not($currentText).removeClass('active');

      /* Toggle the active class here */
      $currentText.toggleClass('active');
  });
})(jQuery); // jQuery passed to IIFE function
ul {
    list-style: none;
}

.accordion-wrapper .title {
    border: 1px solid #0F0F0F;
    background: #CCCCCC;
    font-size: 14px;
    margin: 0 10px 0 10px;
    padding: 6px 8px;
    cursor: pointer;
}

.accordion-wrapper .text {
    display: none;
    font-size: 12px;
    padding: 8px 16px;
    text-align: justify;
}

.accordion-wrapper .active {
    display: block;
}

.functionality {
    font-size: 16px;
    margin: 12px 8px;
    padding: 6px 8px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
    <div class="functionality">Accordion</div>
    <ul class="accordion-wrapper">
        <li>
            <div class="title">Title</div>
            <div class="text">Description</div>
        </li>
        <li>
            <div class="title">Title</div>
            <div class="text">Description</div>
        </li>
        <li>
            <div class="title">Title</div>
            <div class="text">Description</div>
        </li>
    </ul>
</div>

Answer №2

To streamline this process, simply toggle the class .active on the element with the class .title, then utilize the CSS sibling selector to toggle the visibility of the element with the class .text.

(function($) {
  $(function() {
    function accordion() {
      var $clicked = $(".title");
      var $text = $(".text");

      $clicked.click(function() {
        $clicked.not($(this)).removeClass('active');
        $(this).toggleClass('active');
      });
    }
    accordion();
  });
})(jQuery);
ul {
    list-style: none;
}

.accordion-wrapper .title {
    border: 1px solid #0F0F0F;
    background: #CCCCCC;
    font-size: 14px;
    margin: 0 10px 0 10px;
    padding: 6px 8px;
    cursor: pointer;
}

.accordion-wrapper .text {
    display: none;
    font-size: 12px;
    padding: 8px 16px;
    text-align: justify;
}

.active + .text {
    display: block;
}

.functionality {
    font-size: 16px;
    margin: 12px 8px;
    padding: 6px 8px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
    <div class="functionality">Accordion</div>
    <ul class="accordion-wrapper">
        <li>
            <div class="title">Title</div>
            <div class="text">Description</div>
        </li>
        <li>
            <div class="title">Title</div>
            <div class="text">Description</div>
        </li>
        <li>
            <div class="title">Title</div>
            <div class="text">Description</div>
        </li>
    </ul>
</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 is preventing this PHP script from functioning properly with Google Maps?

I'm having trouble understanding why this PHP script isn't generating the specified map on the HTML page. Any suggestions? <!doctype html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-sc ...

What is the best way to include a new user in my list of friends within the User schema?

Working on my customized social media platform, I have implemented a feature where users can send friend requests by clicking on a button. <form action="/requests" method="POST"> <input type="hidden" name="send ...

A guide on transforming HTML into a variable using JavaScript

Having trouble converting HTML to a JavaScript variable. This is my HTML code: <div onclick="openfullanswer('2','Discription part','This is the code part');"> I want to create this HTML code dynamically, but I&apo ...

AmCharts Axis renderer mistakenly renders an additional grid line

I have successfully created a basic XYChart using amcharts4. To get rid of the grid lines on the x-axis, I changed the stroke opacity for the x-axis grid to 0 using the following code: xAxis.renderer.grid.template.strokeOpacity = 0; Everything was workin ...

Unable to retrieve custom CSS and JS files hosted on the server

Encountering Server Issue My server is returning a 404 NOT FOUND error when trying to access my static files: css and js. In IntelliJ IDEA, the path appears correct as shown in the image https://i.stack.imgur.com/nTFv9.png. However, upon accessing the pa ...

The process of extracting all arrays from a JSON object

Within my JSON object, there is a list of countries each with multiple regions stored in an array. My goal is to extract and combine all the regions into one single list. However, when I attempt to map the data, it does not consolidate all the regions as e ...

When creating nested objects, the defineproperty function on the object prototype does not trigger

Exploring and experimenting to unravel the workings of this feature, but I find myself puzzled by this particular scenario. Object.defineProperty(Object.prototype, 'a', {set: function() {console.log("Set!");} }); Based on my understanding, when ...

Split a string into chunks at every odd and even index

I have limited knowledge of javascript. When given the string "3005600008000", I need to devise a method that multiplies all the digits in the odd-numbered positions by 2 and those in the even-numbered positions by 1. This code snippet I drafted seems to ...

Using AJAX in a knockoutjs Form Demonstration

I need help creating a basic form using Knockoutjs to send user information via AJAX. Is there an example available that includes three fields (firstname, lastname, postal-code)? The form should either populate with values from the server if available or d ...

CSS prefers to remain within its original controller and does not want to be uploaded from any other source

My webpage includes headers and footers with CSS in them. Strangely, when I load the view using one controller, the CSS displays correctly. But when I try to load the same view using a different controller, the CSS doesn't appear at all. What could b ...

Step-by-step guide on activating and utilizing file versioning in Firebase Storage

Google Cloud Platform powers Firebase storage, allowing for versioning of files. Within the Firebase console, there are no options related to the GCP bucket. If you access the GCP console, it may not be apparent how to enable versioning in the bucket asso ...

Ways to identify when a file download has finished with the help of javascript

let pageUrl = "GPGeneration_Credit.ashx?UniqueID=" + __uniqueId + "&supplierID=" + supplierID + "&CreditID=" + OrderIds; window.open(pageUrl); // Want to check if the file download is complete and then refresh the page location.r ...

Troubleshooting: CSS3 transform issue unresolved

I'm attempting to add a 10-degree rotation to my menu items. While this CSS effect works in Firefox, I can't seem to get it to work in Chrome and Safari. I already know that IE doesn't support this particular CSS3 property, so that's no ...

Ways to limit access to the jQuery AJAX method from the browser console window

Looking for ways to prevent unauthorized access through the browser console to manipulate the jQuery ajax method. The issue at hand is that anyone can easily access the console and execute a script to repeatedly make requests, causing potential security r ...

Utilizing jQuery UI slider to specify a range based on time (instead of timeline.js), with a set width

Is it possible to create a continuous slider within a fixed width, accommodating a large number of years while maintaining aesthetics? For example: Slider width: 600px Number of years needed: 100 years (1900-2000) (each mark represents one year) Howeve ...

Struggling to get a price calculator up and running efficiently

The code I have should be functioning, however, when I insert it into the code module in the Divi theme on WordPress, it doesn't work. Interestingly, when I try it in a notepad, it works perfectly fine but fails to function on the website itself. () ...

How to use CSS to dynamically adjust the maximum width of an overflow container based on its children's content

I have a container with an overflowing <div> that displays multiple <ul> <li> lists. Each list always contains the same number of bordered <li> items, resembling a table. However, there may be instances where a <ul> has only ...

Display debugging information in the console using bunyan

child.log.info('info'); child.log.debug('debug'); When running the command: node app.js | bunyan -o short -l debug I am encountering an issue where only INFO messages are displayed and DEBUG messages are not showing up. I want to be ...

Identifying the specific filter used in Vue-tables-2

Trying to configure a basic server-side vue-tables-2 with dual filters - one dropdown and the other a search field. The challenge here is identifying which filter was applied within the requestFunction() in order to send a server request. My current strate ...

Generating a fresh object derived from an existing object without saving any modifications

I am facing an issue with using an Array to store boilerplate objects that can be copied, modified, and added to another Array. The problem arises when the new Array takes ownership of the object, causing changes to persist in the original object. For exa ...