Guide on implementing the addClass method for a :before pseudo-element when clicked

A custom feature has been developed that allows for a slideDown effect when the title is clicked, revealing a new section. Initially, the title displays a plus symbol to indicate that it can be expanded. However, upon clicking the title, the intention is to change the background-image to show a minus icon, signaling that the section has collapsed.

The image manipulation is achieved using the pseudo element :before. In the JavaScript code, the goal is to switch the plus symbol with the minus symbol by adding the opened class with the addClass function. The attempt to add the class is as follows:

.infoTitles:before.open

If there are any insights into what might be going wrong in this setup, please share your thoughts!

$('.faqBlock').click(function() {
  var relative = $(this);
  if (!relative.hasClass('opened')) {
    $('.opened').removeClass('opened').next('.infoFaqContainer').slideUp(500);
    relative.addClass('opened').next('.infoFaqContainer').slideDown();
    $('.infoTitles:before').addClass('opened'); //attempting to add the class for the icon here
  } else {
    relative.removeClass('opened').next('.infoFaqContainer').slideUp(500);
  }
  return false;
});
.faqTitle {
font-size: 1.8rem;
margin-bottom: 50px;
}
.faqCont {
width: 100%;
height: auto;
border-bottom: 1px solid #2E393F;
}
.faqBlock {
width: 100%;
padding: 40px 0;
cursor: pointer;
}
.infoTitles {
color: #2E393F;
font-family: 'Quicksand', sans-serif;
font-size: 1.5rem;
}
.infoTitles:before {
content: '';
vertical-align: bottom;
float: left;
margin-right: 8px;
background-image: url('https://www.colourbox.com/preview/5697410-icon-plus-black.jpg');
background-size: 15px 15px;
width: 15px;
height: 15px;
display: block;
}
.infoTitles:before.open {
content: '';
vertical-align: bottom;
float: left;
margin-right: 8px;
background-image: url('https://cdn3.iconfinder.com/data/icons/glypho-generic-icons/64/minus-big-512.png');
background-size: 15px 15px;
width: 15px;
height: 15px;
display: block;
}
.infoFaqContainer {
display: none;
}
.infoFaqInner {
padding: 0 5px 40px 5px;
}
.faqDesc {
font-family: 'Open-sans', sans-serif;
font-size: 1rem;
/*letter-spacing: .1rem;*/
line-height: 1.5em;
color: #2E393F;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="weddingInfoBlock">
<div class="wInfoBlockCont">
  <span class="wIBCTitle">FAQ's</span>
  <div class="faqCont">
    <div class="faqBlock">
      <div class="infoTitles">Title</div>
    </div>
    <div class="infoFaqContainer">
      <div class="infoFaqInner">
        <p class="faqDesc">
          Text
        </p>
      </div>
    </div>
  </div>
</div>

Answer №1

1) It appears that the incorrect class was used in your style rule - the class should be opened instead of open

2) The class is not applied to the pseudo element as per your CSS structure, it should be applied to the parent.

Essentially, the rule needs to be updated from:

.infoTitles:before.open {

to

.opened .infoTitles:before {

Complete code snippet provided below:

$('.faqBlock').click(function() {
  var relative = $(this);
  if (!relative.hasClass('opened')) {
    $('.opened').removeClass('opened').next('.infoFaqContainer').slideUp(500);
    relative.addClass('opened').next('.infoFaqContainer').slideDown();
    $('.infoTitles:before').addClass('opened'); //this is where I am adding the class for the icon
  } else {
    relative.removeClass('opened').next('.infoFaqContainer').slideUp(500);
  }
  return false;
});
.faqTitle {
font-size: 1.8rem;
margin-bottom: 50px;
}
.faqCont {
width: 100%;
height: auto;
border-bottom: 1px solid #2E393F;
}
.faqBlock {
width: 100%;
padding: 40px 0;
cursor: pointer;
}
.infoTitles {
color: #2E393F;
font-family: 'Quicksand', sans-serif;
font-size: 1.5rem;
}
.infoTitles:before {
content: '';
vertical-align: bottom;
float: left;
margin-right: 8px;
background-image: url('https://www.colourbox.com/preview/5697410-icon-plus-black.jpg');
background-size: 15px 15px;
width: 15px;
height: 15px;
display: block;
}
.opened .infoTitles:before {
content: '';
vertical-align: bottom;
float: left;
margin-right: 8px;
background-image: url('https://cdn3.iconfinder.com/data/icons/glypho-generic-icons/64/minus-big-512.png');
background-size: 15px 15px;
width: 15px;
height: 15px;
display: block;
}
.infoFaqContainer {
display: none;
}
.infoFaqInner {
padding: 0 5px 40px 5px;
}
.faqDesc {
font-family: 'Open-sans', sans-serif;
font-size: 1rem;
/*letter-spacing: .1rem;*/
line-height: 1.5em;
color: #2E393F;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="weddingInfoBlock">
<div class="wInfoBlockCont">
  <span class="wIBCTitle">FAQ's</span>
  <div class="faqCont">
    <div class="faqBlock">
      <div class="infoTitles">Title</div>
    </div>
    <div class="infoFaqContainer">
      <div class="infoFaqInner">
        <p class="faqDesc">
          Text
        </p>
      </div>
    </div>
  </div>
</div>

Answer №2

To target the inner icon within the faqBlock div, use the opened class. For example, if the faqBlock does not have the opened class, display the plus icon in the child selector. Conversely, when the faqBlock has the opened class, show the minus icon instead. Include the following CSS code:

.faqBlock .infoTitles:before {
    content: '';
    vertical-align: bottom;
    float: left;
    margin-right: 8px;
    background-image: url('https://www.colourbox.com/preview/5697410-icon-plus-black.jpg');
    background-size: 15px 15px;
    width: 15px;
    height: 15px;
    display: block;
}

.faqBlock.opened .infoTitles:before {
    content: '';
    vertical-align: bottom;
    float: left;
    margin-right: 8px;
    background-image: url('https://cdn3.iconfinder.com/data/icons/glypho-generic-icons/64/minus-big-512.png');
    background-size: 15px 15px;
    width: 15px;
    height: 15px;
    display: block;
}

View the jsfiddle demo Here

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

Tips for enabling item draggability following AJAX filtering operations

I have a draggable list of names that can be filtered using checkboxes. When a checkbox is checked, an ajax call is made to filter the names. The list is displayed in an accordion format as shown below: <div id="myAccordion"> <?ph ...

What is the best way to access and modify a nested object within a complex object containing an array of objects?

I've got the course structure all sorted out, but I'm struggling to understand how to retrieve a lesson object by its ID and also update a lesson object with new property values. If anyone could provide some guidance on the right approach, it wo ...

I cannot seem to get the image to display properly, it keeps getting cut off and

I am trying to add an image to my website: https://i.sstatic.net/giAdT.jpg However, when I try to do this, the full image is not showing: https://i.sstatic.net/VRBlB.png This is the code I am using: <!DOCTYPE html> <html lang="en"> ...

Generate a new tree structure using the identifiers of list items within an unorganized list

Attempting to convert the id's of li's in a nested unordered list into valid JSON. For example, consider the following list. To be clear, the goal is not to create the UL list itself, but rather the JSON from the li id's <ul class="lis ...

Issue with Three.js Raycaster failing to intersect with a custom vertex shader

I'm currently working on implementing a particle system using a dedicated Vertex shader. I have provided a code example for reference: https://jsfiddle.net/dthevenin/7arntcog/ My approach involves utilizing a Raycaster for mouse picking to enable int ...

Guide to opening all href links in a new window

When loading HTML content parsed from an email to a frame, the issue arises when there is an href link that tries to open in the same frame. I want to modify it so that it opens in a new tab instead. Usually, setting the target to _blank in the href would ...

unfamiliar gap in the absence of jquery's creation

Trying to create a form similar to this example: https://jsfiddle.net/zycyuyrz/ The issue is that the HTML code for static rows and dynamically generated jQuery rows appears different. Any idea why? HTML: <div class="container"> <fieldset ...

Issue: Attempting to display array elements within a React Functional Component?

Encountering an issue while attempting to log array elements, receiving the error message: TypeError: Cannot read property '0' of undefined Interestingly, I am able to successfully log the project.tasks array object in the console. However, l ...

Employing setTimeout for hover interactions in conjunction with a tooltip

I have created an SVG map consisting of hexagons grouped together. My goal is to display a tooltip when the user hovers over a group, but with a 3-second delay. If the user moves away before the delay ends, I want to clear that delay to prevent the tooltip ...

Tips for filling rotated canvas rectangles with images

I have a canvas with 4 rectangles and I would like to fill each rectangle with an image, for example i1.png. The canvas has been rotated and scaled, causing the images to not be in their correct positions. How can I resolve this issue? Here is the code : ...

The clash between mototools and JavaScript is causing both to be non-operational

I am currently facing a conflict between JavaScript and Mototools in my project. I have heard about the NoConflict script, but I'm not sure how to implement it properly. I will provide the code for both dependencies so that someone can explain it to m ...

Modify the bootstrap dropdown background to display only an image

I am looking to make the image in a dropdown/dropup menu fill the entire background of the dropdown. Currently, there is still white space visible around the image. How can I ensure that the dropdown shows only the image and includes a scroll wheel? Addit ...

Stopping the use of <script> tags within HTML form submissions

After launching my website at , I discovered that a user is attempting to redirect it to different websites using HTML form input tags. While I'm unsure of the exact method they are employing, I am seeking a way to block users from entering these nefa ...

The alignment issue arises when using Grid-column-end with items of a set width

I am facing an issue with aligning an item in a specific column while maintaining a fixed width. When I attempt to right-align the item, it overflows the designated space. Removing the width attribute resolves the problem, but that is not a viable solution ...

Using jQuery to dynamically add or remove table rows based on user inputs

Apologies if this is too elementary. I am attempting to insert rows into a table if the current number of rows is less than what the user requires. Simultaneously, I need to remove any excess rows if the current number exceeds the user's specificati ...

Quickest method of writing this (using jQuery)

Is there a more efficient way to accomplish this task in one line? var $d = $('<div>').addClass('clear'); $('.clearAfter').after($d); Alternatively, you can add the CSS style directly using: $('.clearAfter') ...

What is preventing me from inserting a variable into quotes without needing to use a plus sign?

I'm attempting to create a sub-pattern and display it on the screen using res.send as shown below: app.get('/r/:something', (req, res) => { const { something } = req.params; res.send('<h1>Viewing ${something} on the scr ...

Utilizing jQuery on the newly inserted <tr> element

I have created a jQuery code that includes 3 pre-existing rows with a text box to add more similar rows. The jQuery functions are initially applied to the first 3 hard-coded rows. I am looking to extend this functionality to dynamically added rows later on ...

Struggling to generate a cookie through an express middleware

I'm currently working on setting up a cookie for new user registrations in my app to track their first login attempt. I came across this thread which provided some guidance but I'm still facing issues. Below is the snippet of my code: // Middle ...

Transform a 3D text rotation JavaScript file into an Angular component tailored TypeScript file

I have a javascript file that rotates text in 3D format, and I need help converting it into an Angular component specific TypeScript file. You can find the codepen for the original JavaScript code here. Below are my Angular files: index.html <!doctyp ...