Interested in collapsing the side panel by clicking on the minus symbol?

There is an issue with the sidepanel functionality, where the PLUS symbol changes to a minus when the panel is open but doesn't revert back to PLUS when closed. Additionally, I would like users to be able to click the minus symbol to close the sidepanel as well.

If it's not clear, you can check out this live example on JsFiddle: https://jsfiddle.net/bob_js/h9yfbden/1/

Here is the HTML code snippet:

<div class="container">
  <i class="glyphicon glyphicon-plus-sign cd-btn"></i>
</div>
<div class="cd-panel-nvv from-right"> 
            <header class="cd-panel-header">
                <a href="#0" class="cd-panel-close"></a>
            </header>
            <div class="cd-panel-container">
        Content
  </div>
</div>

CSS related to the icons and styles:

.glyphicon-plus-sign, .glyphicon-minus-sign{
  /* CSS styling for plus and minus symbols */
}
/* More CSS styles here */

The jQuery script used for toggle functionality:

$(".glyphicon-minus-sign, .glyphicon-plus-sign").click(function(){
    $(this).toggleClass("glyphicon-minus-sign glyphicon-plus-sign");
});

jQuery(function($){
    //open the lateral panel
    $('.cd-btn').on('click', function(event){
        event.preventDefault();
        $('.cd-panel').addClass('is-visible');
    });
    //close the lateral panel
    $('.cd-panel').on('click', function(event){
        if( $(event.target).is('.cd-panel') || $(event.target).is('.cd-panel-close') ) { 
            $('.cd-panel').removeClass('is-visible');
            event.preventDefault();
        }
    });
});

Answer №1

It turns out you were pretty close!

All I did was switch addClass to toggleClass
in $('.cd-btn').on('click'....

Plus, I included

$('.cd-btn').toggleClass("glyphicon-minus-sign glyphicon-plus-sign");

in $('.cd-panel').on('click'....

$(".glyphicon-minus-sign, .glyphicon-plus-sign").click(function(){
    $(this).toggleClass("glyphicon-minus-sign glyphicon-plus-sign");
});

//NVV
jQuery(function($){
    //open the lateral panel
    $('.cd-btn').on('click', function(event){
        event.preventDefault();
        $('.cd-panel').toggleClass('is-visible');
    });
    //close the lateral panel
    $('.cd-panel').on('click', function(event){
        if( $(event.target).is('.cd-panel') || $(event.target).is('.cd-panel-close') ) { 
            $('.cd-panel').removeClass('is-visible');
            $('.cd-btn').toggleClass("glyphicon-minus-sign glyphicon-plus-sign");
            event.preventDefault();
        }
    });
});

Check out the updated Fiddle

Answer №2

To toggle between classes, you can use .toggleClass(); and listen for both $('.cd-btn, .cd-panel').

Check out the JSFiddle demo here

jQuery(function($) {
  // Open or close the side panel
  $('.cd-btn, .cd-panel').on('click', function(event) {
    event.preventDefault();
    $('.cd-panel').toggleClass('is-visible');
    $('.cd-btn').toggleClass("glyphicon-minus-sign glyphicon-plus-sign");
  });
});

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

Implementing dynamic active class changes in a navbar with JavaScript

My goal was to have the navbar change its class to 'active' dynamically when a user clicks on the <li> tag. Can you help me pinpoint where I made a mistake? dynamicNavbar(); function dynamicNavbar() { $('.nav_w3ls .menu a'). ...

React - onchange event for input checkboxes does not trigger on the initial click

The behavior of a checkbox input is proving to be quite unreliable. Currently, I am setting the checkbox state based on the store's state, which generally works well. However, there are instances where checking the checkbox does not trigger the oncha ...

What is the most effective method to exhibit every element within a content wrapper at a specific interval of time?

I am looking for a way to display each div within the content-wrapper after a specific time interval. Currently, I am using individual classes like el1, el2, el3, ... to accomplish this task. However, when dealing with content-wrappers containing multipl ...

Arrange the menu items in a column layout based on a given condition

Can someone assist me with displaying the menu subitems? I have created a plunker. Take a look at it to understand what I need (open plunker in full screen) https://plnkr.co/edit/IMEJFPfl5kavKvnUYaRy?p=preview In the plunker above, there are two dropdown ...

Running an aggregate query in mongo_go_driver that includes javascript code: an easy guide

Currently, I am utilizing the mongo-go-driver () and attempting to achieve a similar action as below: db.getCollection('mycollection').aggregate([ { $lookup: { from: "anothercollection", localField: "_id", foreignFiel ...

Allowing Cross-Origin Resource Sharing on an Express Server hosted using Firebase Cloud Functions

Hey there, I have a simple Express setup on Firebase, shown below: import { onRequest } from 'firebase-functions/v2/https'; import express from 'express'; import bodyParser from 'body-parser'; import router from './router ...

Obtaining a variety of data points from an XMLHttpRequest

I recently discovered a technique for sending multiple variables on SO. Here is the code snippet I used: xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET","http://127.0.0.1:3000?var1=" + name + "&var2=test", true); xmlhttp.send(); xmlhttp.onreadystat ...

Substitute all numerical values with a designated number from a variable

I came across a link that looks like this foo.net/index.php?page=15 My goal is to replace any number after page=xxx and retrieve the new number from a variable Currently, my code only replaces 15 with 16 var num = 16, // What if the str = foo.net/index ...

Using a custom filter in AngularJS allows for seamless data filtering directly from the initial dataset

My goal is to implement a custom filter that will allow me to filter data based on a search word. The scope attribute gets populated in the controller's scope as shown below: naApp.controller('naCareNewTicketCtrl', ['$scope', &apo ...

Having trouble displaying the accurate model value in the alert

I have the following piece of code: $(document).ready(function () { var edit= @Model.Edit.ToString().ToLower(); if( edit !=true ) { $("#editWriteup").hide(); } var date= @Model.EndDate; alert(date); ...

stop all HTML5 videos when a specific video is played using jQuery

I am facing an issue with 10 HTML5 videos on the same page, CHALLENGE Whenever I click on different videos, they all start playing simultaneously... <div><video class="SetVideo"></video><button class="videoP"></button>< ...

I'm unable to provide an ID value within the modal section

<div class="table"> .... <tr *ngFor="let product of products; index as i"> <th scope="row">{{ i + 1 }}</th> <td>{{ product.name }}</td> <td>{{ product.category }}</td> <td> ...

Issue with displaying Font Awesome icons in Bootstrap 4 navbar brand

Recently, I've been setting up a local Laravel website to explore the latest features and changes in Laravel 5.6. However, I've encountered an issue while trying to integrate a Font Awesome icon into the navbar-brand of Bootstrap 4. The icon does ...

Explore the interactive feature of hovering over a component within a div that is enhanced with a transformative translate3d effect upon hover. Discover the seamless event queuing

Within a div, there is a modal component that includes a transform: translate3d effect with hover transition. The div is created using vue.js list rendering method. Upon opening the modal and hovering over it, it jerks between the parent div and its intend ...

Change the background color of the selectInput component in Shiny to fit your

Take a look at this sample shiny app: library(shiny) ui <- fluidPage(tags$head(includeCSS("www/mycss.css")), selectInput("foo", "Choose", width = '20%', multiple = F, selected = "red1", choices = list(red = c ...

A step-by-step guide to customizing the default ID and class of WordPress navigation menus

When it comes to the generation of HTML for Wordpress menus, the structure typically looks something like this: <ul id="main-menu"> <li> home </li> <li> About </li> ...... ... <ul class = "sub-menu"> ...

Regular expression patterns for authenticating and verifying passwords

Currently, I am working on a JavaScript program to validate passwords using regex. Here are the requirements: The password must consist of at least seven characters. It should include at least one of the following: an uppercase letter (A-Z) a lowercas ...

Unable to execute commitlint in husky along with a different custom command

Is it possible to set up two precommit hooks with husky? Specifically, I want to integrate commitlint along with a custom script specified in my package.json. After installing husky and creating a pre-commit script in the .husky folder, here is what I have ...

Trigger the React useEffect only when the inputed string matches the previous one

Currently, I am in the process of creating my own custom useFetch hook. export const useFetch = <T extends unknown>( url: string, options?: RequestInit ) => { const [loading, setLoading] = useState(false); const [error, setError] = ...

Discovering data in MongoDB nested arrays

My aim is to: Retrieve all documents with status=true. Filter and only display objects with active=true in the life array. Shown below are examples of my MongoDB documents: { "name": "justine", "life" : [ { ...