Switch the sign from plus to minus by modifying the :before element

My goal is to change the plus sign to a minus sign when it is collapsed by modifying the plus:before (by setting display to none or something similar to create a minus sign effect.) Currently, my code is functioning properly and collapsing as intended. You can view a demo here.

HTML

<div class="box">
  <div class="box-item">
    <p>Item #1<span class="plus"></span></p>
    <ul>
      <li>List Item #1</li>
      <li>List Item #2</li>
    </ul>
  </div>

  <div class="box-item">
    <p>Item #2<span class="plus"></span></p>
    <ul style="">
      <li>List Item #1</li>
      <li>List Item #2</li>
    </ul>
  </div>

  <div class="box-item">
    <p>Item #3<span class="plus"></span></p>
    <ul style="">
      <li>List Item #1</li>
      <li>List Item #2</li>
    </ul>
  </div>

  <div class="box-item">
    <p>Item #4<span class="plus"></span></p>
    <ul style="">
      <li>List Item #1</li>
      <li>List Item #2</li>
    </ul>
  </div>

  <div class="box-item">
    <p>Item #5<span class="plus"></span></p>
    <ul style="">
      <li>List Item #1</li>
      <li>List Item #2</li>
    </ul>
  </div>
</div>

jQuery

$(document).ready(function () { 
  $('.box p').click(function(){
    var target = $(this).parent().children('.box ul');
    if (!target.is(":visible")) {
      $('.box ul').slideUp();
      $(target).slideDown();
    } else {
      $(target).slideUp();
    }
  });
});

Answer №1

Suppose you've created a CSS class called minus that functions similarly to the plus class, but instead displays a minus sign.

$(document).ready(function () { 
  $('.box p').click(function(){
      var target = $(this).parent().children('.box ul');
      if (!target.is(":visible")) {
        $('.box ul').slideUp();
        $(target).slideDown();
        $(this).children('span').removeClass('plus').addClass('minus');
      }
      else {
        $(target).slideUp();
        $(this).children('span').removeClass('minus').addClass('plus);
      }
    });
});

By the way, have you considered just setting the css content property of the span to '+' (or '-') instead?

Answer №2

You must include the class minus and alternate it with the plus class during every expand-collapse action on the item.

Visit CodePen

$(document).ready(function () { 
  $('.box p').click(function(){
      var target = $(this).parent().children('.box ul');
      if (!target.is(":visible")) {
        $('.box ul').slideUp();
        $(target).slideDown();
        $(this).children(".box .plus").toggleClass("plus, minus");
      }
      else {
        $(target).slideUp();
        $(this).children(".box .plus").toggleClass("plus, minus");
      }
    });
});
.box {
  font-family: Arial, sans-serif;
  border-radius: 2px;
  margin-bottom: 30px;
  padding: 20px;
  color: #444;
  background-color: #EEE;
  width: 400px;
}
.box p {
    color: #;
    display: block;
    font-size: 22px;
    text-transform: uppercase;
    cursor: pointer;
}
.box ul {
    display: none;
}
.box li {
    color: #666;
    padding-left: 12px;

}
.box .plus, .box .minus {
     color: #3BB1D0;
    cursor: pointer;
    float: right;
    position: relative;
    right: 14px;
    bottom: -6px;
}
.box .plus:before {
    content: '';
    display: block;
    left: 7px;
    width: 4px;
    height: 21px;
    margin-top: 0;
    background: #444;
    border-radius: 2px
}
.box .plus:after {
    content: '';
    display: block;
    left: -8px;
    width: 21px;
    height: 4px;
    top: 8px;
    background: #444;
    border-radius: 2px;
    position: absolute
}
.box .minus:before {
    content: '';
    display: block;
    left: 7px;
    width: 4px;
    height: 0;
    margin-top: 0;
    background: #444;
    border-radius: 2px
}
.box .minus:after {
    content: '';
    display: block;
    left: -8px;
    width: 21px;
    height: 4px;
    top: 8px;
    background: #444;
    border-radius: 2px;
    position: absolute
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box">
  <div class="box-item">
    <p>Item #1<span class="plus"></span></p>
    <ul>
      <li>List Item #1</li>
      <li>List Item #2</li>
    </ul>
  </div>

  <div class="box-item">
    <p>Item #2<span class="plus"></span></p>
    <ul style="">
      <li>List Item #1</li>
      <li>List Item #2</li>
    </ul>
  </div>

  <div class="box-item">
    <p>Item #3<span class="plus"></span></p>
    <ul style="">
      <li>List Item #1</li>
      <li>List Item #2</li>
    </ul>
  </div>

  <div class="box-item">
    <p>Item #4<span class="plus"></span></p>
    <ul style="">
      <li>List Item #1</li>
      <li>List Item #2</li>
    </ul>
  </div>

  <div class="box-item">
    <p>Item #5<span class="plus"></span></p>
    <ul style="">
      <li>List Item #1</li>
      <li>List Item #2</li>
    </ul>
  </div>
</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

When HTML/JS code is utilized, the submit button or image should function to open the same page as

In addition to the left and right mouse buttons, you also have a middle mouse button. If you click on an image or hyperlink with this middle mouse button while browsing any website, it will open a new window in the background. Now, I want to achieve a sim ...

Wrap each object in a container and then insert its key and values into that container using jQuery

How can I wrap each item and then insert the object's indexes and values into each created wrapper? I've attempted to accomplish this using the following code: $.ajax({ url: "some url", type: "GET", success: function(data) { var data ...

Discrepancy in sorting order of key-value objects in JavaScript

Check out my jsFiddle demonstration illustrating the issue: Example Here is the HTML structure: <select id="drop1" data-jsdrop-data="countries"></select> <select id="drop2" data-jsdrop-data="countries2"></select>​ Below is the ...

Remove the blue outline in Fancybox when clicked

Does anyone know how to remove the default blue outline that appears after opening and closing an image or video in Fancybox 3? It disappears when clicked next to, but I would like to get rid of it completely. Any help is appreciated. https://i.stack.imgu ...

Problem encountered during the transfer of JSON data from PHP to JavaScript

Currently, I am working on a PHP file that extracts data from a database to display it on a chart using the Chart.js library. The chart is functioning properly, but I am facing an issue where I need to use the json_encode() function to pass the array value ...

Input field for postal code containing only numbers (maximum 5 digits) in Angular version 4/5

I am struggling with creating an input field that accepts numbers. If I use type="text", I can only type 5 characters of alphanumeric data, but if I use type="number", it allows any number input without limiting it to just 5 numbers. Thank you in advance f ...

HTML5's drag-and-drop feature, including nested targets, allows for intuitive and interactive user

I'm currently implementing HTML5 drag and drop functionality. I have a parent div that is droppable, and inside it, there is another child div that is also droppable. <div id="target-parent"> <div id="target-child"></div> </d ...

What is the best way to update a form after it has been submitted?

i have a form that looks like this <form id="abc"> <div> <select > <option id= "1"> ha1 </option> <option id= "1"> ha2 </option> <option id= "1"> ha3 </option> <option id= "1"> ha4 </option> ...

Customizing checkboxes in React with JSS: A step-by-step guide

I'm currently working on customizing CSS using JSS as my styling solution, which is proving to be a bit complex while following the w3schools tutorial. https://www.w3schools.com/howto/howto_css_custom_checkbox.asp HTML: <label class="container"& ...

Tips for launching a fresh window and embedding HTML into it with jQuery?

I'm attempting to use JavaScript to open a new window, but the HTML content is not being added: var callScriptText = $('#callScriptText').html(); var url = '/Action/CallScript/?callScript='; // Open the current call script in a n ...

How come the 'event' variable can still be accessed even without being explicitly passed as a parameter?

It's intriguing to think about why this code behaves differently in certain browsers. For example, even when there isn't a parameter passed to the click() function, the event variable still exists and the dosomething method is called on the event ...

What is the best way to ensure that the video width is fully responsive at 100%?

Looking for help with making a Shopify website more responsive. I have a hero video at the top, but it's leaning to the right and leaving a blank space on the left. Can anyone assist me in making it 100% responsive without much coding knowledge? I&apo ...

What are the methods for choosing various boxes using the UP/DOWN/RIGHT/LEFT arrow keys?

This code snippet displays a 3x3 matrix where boxes can be hovered over and selected. The goal is to navigate around with keys and select boxes using ENTER. Can anyone provide guidance on how to achieve this functionality? <link rel="stylesheet" href ...

Best method to toggle the visibility of elements based on various conditions

In an effort to improve the loading time of my products, I am looking for ways to optimize the display of a large list of products based on user-selected conditions. To simplify things, I have narrowed it down to three conditions: product name, size, and c ...

CSS: The cell borders seem to have lost their rounded corners inexplicably

I am having trouble creating a navigation bar with rounded borders on the end cells. Despite seeing some rounding in the background, the borders remain stubbornly sharp at the corners. You can view the issue here: http://jsfiddle.net/7veZQ/299/ Below is ...

Is the CSS3 bar chart flipped?

I'm currently developing a responsive bar chart, and everything seems to be going smoothly except for one issue - when viewing it in full screen, the bars appear to be flipped upside down. It's quite puzzling as all other elements such as text an ...

What steps should I take to address this bug in my Django HTML template?

I've encountered a strange bug with Django HTML templates where everything appears to be placed within the body tag, regardless of my efforts. There is also some invisible text that throws off the page alignment. Here is how my browser displays the S ...

Encountered a problem while executing CSS in Shiny R

I am attempting to incorporate my css file into Shiny R. I have been able to run the application without any issues before adding the style.css file. Below are the steps I have taken: library(shiny) library(tidyverse) library(leaflet) library(leaflet.extra ...

When attempting to upload a file from the client side using a jQuery AJAX call, the selected file is not

I have been browsing posts related to this topic, but so far no one has been able to help me. I am currently attempting to upload a file selected in a client-side input field, and then call a server method using a jQuery Ajax request. Here is how I am doin ...

PHP 5's HTML Form Library

Currently in search of an alternative to QuickForm. I have encountered performance problems with QF, specifically when dealing with a large number of options in combobox. I prefer something that is more object-oriented like Zend_Form, but without the exc ...