JavaScript function to add or remove a class upon clicking

Currently experiencing an issue with my accordion functionality. I have successfully implemented code that allows for toggling one heading open at a time, but I am struggling to add an open/close image beside each heading.

At the moment, when a heading is clicked, the 'open' image is removed and a 'close' image is displayed. Now, I need to find a way to swap these classes back so that when a different heading is toggled, the appropriate image is displayed.

Below is the code I am utilizing:

JavaScript

<SCRIPT>
    $("#accordion > li").click(function () {
        $("#accordian li").removeClass("faq-header");
        $(this).addClass("faq-header2");
        if (false == $(this).next().is(':visible')) {
            $('#accordion > ul').slideUp(250);
            $('#accordion > ul').addClass('faq-header');
            $(this).removeClass("faq-header");
        }
        $(this).next().slideToggle(300);
    });

    $('#accordion > ul:eq(0)').show();
</SCRIPT>

CSS

#accordion {
    list-style: none;
    margin-left:-38px;
}
#accordion ul:eq {
    background-image:url(../img/faq-open.gif);
    background-repeat:no-repeat;
    background-position:right;
    padding-right:20px;
}
#accordion li{
    display: block;
    background-color: #FFF;
    font-weight: bold;
    cursor: pointer;
}
.faq-header {
    text-align:left;
    background-image:url(../img/faq-close.gif);
    background-repeat:no-repeat;
    background-position:right;
    margin-right:20px;
}
.faq-header2 {
    text-align:left;
    background-image:url(../img/faq-open.gif);
    background-repeat:no-repeat;
    background-position:right;
    margin-right:20px;
}
#accordion ul {
    list-style: none;
    display: none;
}
#accordion ul li{
    font-weight: normal;
    cursor: auto;
    background-color: #fff;
    border-bottom:1px solid #999;
    margin-left:-38px !important;
}

I have successfully swapped out classes by using

$("#accordian li").removeClass("faq-header");
to remove one class and $(this).addClass("faq-header2"); to add another. However, I am now facing the task of removing .faq-header2 and adding back .faq-header once the selected section is no longer active. It seems like a straightforward if function is needed, but I am having trouble figuring out the code for it. Any help would be appreciated.

Answer №1

The jQuery UI accordion is a reliable cross-browser widget that displays images on open and close (default is on the left, but can be changed with a simple CSS tweak).

For more information, visit this link.

If you prefer not to use the accordion, an alternative option is to explore toggleClass, which can be found here.

EDIT

Thank you for sharing your HTML. Although I didn't mean for you to provide the entire page, I appreciate it. Upon reviewing your structure, it seems a bit excessive for just an accordion. There are some issues like nesting ul tags within ul tags, which is not standard practice. Consider simplifying your structure for easier jQuery implementation. Here's an example:

<div id="accordion">
    <h3>First header</h3>
    <div>First content</div>
    <h3>Second header</h3>
    <div>Second content</div>
</div>

Regarding your current issue, the following code worked for me:

    $("#accordion > li").click(function () {              
            var self = $(this);
            self.next('ul').slideToggle(300, function () {
                if ($(this).is(':visible')) {
                    self.removeClass('faq-header').addClass('faq-header2')
                }
                else {
                    self.removeClass('faq-header2').addClass('faq-header')
                }
            });
            self.siblings().removeClass('faq-header2').addClass('faq-header').next('ul').slideUp(250);
        });

In your case, toggleClass may not be as effective as expected for adding and removing classes based on your requirements.

Answer №2

In my opinion, the toggle() function can be a little confusing. I prefer to use my own method called replaceClass() to achieve the same result:

void replaceClass(Object classList, String surrogateClass, String classToReplace);

This is how it is defined:

function replaceClass(elementClassList, firstClass, secondClass) {
 if(elementClassList.contains(firstClass)){
 {
  elementClassList.remove(firstClass);
elementClassList.add(secondClass);
}
}

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

Guide to invoking a server-side function through JSON data?

Here is the code I am working on: <script type="text/JavaScript> var myarray = new array(); function getsvg1() { $.ajax({ alert("hello"); type: "post", url: "WebForm1.aspx/getsvg1", ...

The title attribute in Vue3 is updated through props, but computed properties remain unaffected

I incorporated an external library into my project using Vue3. The component I am utilizing is sourced from a third-party library [Edit: Upon realizing that the GitHub repository for this library is no longer being maintained, I have updated the code to re ...

Can you set up an automatic redirect after a payment is made on paypal.me?

Is there a way to automatically redirect users to a "successful payment" landing page after they make a payment on paypal.me? While PayPal offers an "Auto Return" feature, I am uncertain if it is applicable for paypal.me or if there are alternative methods ...

TinyMCE's Textarea is blank and not displaying any content

I can't seem to figure out where I went wrong with using TinyMCE. I downloaded it from and chose version 4.0.3. After downloading, I ended up with a folder called tinymce_4.0.3 which contained subfolders like tinymce_4.0.3\tinymce\js\t ...

Python script for downloading audio files loaded with JavaScript

I am currently working on a Python script to automate the process of downloading English audio files from a specific website. When I click on the play button, the audio file starts to load and play, but I am unsure how to capture and download the file in ...

Is there a way to determine the location where my website is fetching CSS and media files from?

After implementing Wagtail on my website, I encountered an issue with the CSS and images not being found when the site was put into production. I have already tried running python manage.py collectstatic and ensured that all of my CSS files are located in ...

The mysterious case of the vanishing close button on Curator.io's mobile

Currently, I am using curator.io aggregator to showcase my Instagram feed on the website. An issue arises when switching to mobile view as the close button (class="crt-close") disappears. You can see an example of this here: To replicate the pr ...

Challenges with the height of the sticky footer in Bootstrap 4

I'm currently in the process of constructing a website using bootstrap 4 that features a footer with 3 columns. Everything looks great when the browser window is large, such as on a PC, but when it's compressed, like on a mobile phone, only a por ...

Dropped down list failing to display JSON data

I created a website where users can easily update their wifi passwords. The important information is stored in the file data/data.json, which includes details like room number, AP name, and password. [ {"Room": "room 1", "AP nam ...

How to Fetch Byte Image with Ajax and Display it on the Client Side in ASP.Net MVC

After converting an image to bytes, I am facing the challenge of displaying the byteImage in a browser. I have two sets of data, one being the Image ID and the other being the Image_name, which are displayed in a table. However, I am unable to display the ...

Choosing to collapse a nested JSON arrangement in a targeted manner

Facing a challenging problem here and feeling clueless on how to tackle it. Any guidance or pointer in the right direction would be highly appreciated. My dataset looks like this: data = { "agg": { "agg1": [ { "keyWeWant": " ...

Turn off the automatic resizing of Internet Explorer 11 to fit the width of the viewport - IE11

Is there a way to stop Internet Explorer 11 from automatically zooming to fit the viewport width? I need the browser to respect my max-width rule and not scale the content to fill the entire width of the viewport on its own. This issue is happening in IE ...

Could a commenting feature be seamlessly integrated directly into the video player, accessible with a simple click?

I'm exploring the idea of developing a video player that allows users to add comments directly from the player itself. Imagine this: the typical toolbar at the bottom includes standard features like seek bar, volume control, play/pause buttons, but wi ...

typescript error caused by NaN

Apologies for the repetitive question, but I am really struggling to find a solution. I am facing an issue with this calculation. The parameters a to g represent the values of my input from the HTML. I need to use these values to calculate a sum. When I tr ...

Tips for improving the loading speed of Ajax applications

Having an issue with the speed of loading AJAX pages on my main.php and ajx_receipt_sub_detail.php. Looking for suggestions to increase the loading speed of AJAX. Main.php <script src="scripts/jquery-1.11.0.min.js"></script> <script> f ...

What could be the reason my code isn't successfully performing addition within the input field?

As a novice, I am practicing by attempting to retrieve a number from a text field, prompting the user to click a button that adds 2 to that number, and then displaying the result through HTML. However, I keep encountering an issue where NaN is returned whe ...

angular trustAsHtml does not automatically insert content

Two divs are present on the page. Upon clicking button1, an iframe is loaded into div1. The same applies to button2 and div2. These iframes are loaded via ajax and trusted using $sce.trustAsHtml. This is how the HTML looks: <div ng-bind-html="video.tru ...

Modify the content and display of a stationary div based on vertical positions

I am interested in creating a vertical website that features multiple divs appearing at specific y-points on the page. These divs will contain text and will always be positioned fixed at 20% from the left and about 250px from the top. My goal is to have t ...

Adjust the size of the input field as text is being typed in

Can a text input box be automatically resized as text is entered? I've been looking online but haven't come across any solutions. ...

What is the best way to prevent a sticky element from scrolling along with the

I am looking to prevent a sticky div from taking up space on the website. It currently sticks to the desired position as expected. https://i.sstatic.net/wG4SK.png HTML <div v-if="isToastActive" class="toast"> <span clas ...