What is the best way to switch the toggle identifier in order to modify the CSS class "before" using Unicode characters?

I have a compilation of Frequently Asked Questions, accompanied by some custom CSS styling. I am looking to modify the unicode character before each question from a + sign to a - sign, specifically changing content:" \ff0b "; to content:" \002D ";. Below is my code snippet along with the corresponding HTML and CSS:

.faquestion {
    cursor: pointer;
    color: #fff;
    border-left: 12px solid rgb(3,59,76);
    border: 2px solid #fff9e6;
    background-color:rgb(3,59,76);
    border-radius: 4px;
    padding: 10px 10px 10px 15px;
    vertical-align: bottom;
}
.faquestion:hover{
            color:#000;
            border:2px solid rgb(0,87,152);     
            background-color:#DDCBA4;
        }
.faquestion::before{
    content:" \ff0b ";
    padding-right:8px;
    font-weight:bold;
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
function toggler(divId) {
    $("#" + divId).toggle();
}
</script>
<p class="faquestion" onclick="toggler('answer1');"><strong>How do I go to there?</strong></p>
<p class="" id="answer1" style="padding-left: 30px; display: none;">Follow the yellow brick road.</p>

<p class="faquestion" onclick="toggler('answer2');"><strong>What is a virtual experience?</strong></p>
<p class="" id="answer2" style="padding-left: 30px; display: none;">The Virtual experiences is...</p>

It seems like I need to make an adjustment in the toggle code, but I'm unsure about what exactly needs to be added. Any guidance would be greatly appreciated! https://jsfiddle.net/nr3tvaf2/

Answer №1

Allow me to propose two solutions:

The initial approach involves utilizing the attr() for setting the content: '' parameter within the pseudo-class :before. Implement this in your CSS:

.faquestion::before {
    content: attr(before-content);
    ...
}

Additionally, include a supplementary jQuery script that enables you to switch between the icons - and +:

$(element).attr("before-content", function (index, attr) {
    return attr == " +" ? " - " : " + ";
});

Set the default icon as +:

$(document).ready(function () {
    $(".faquestion").attr("before-content", " + ");
});

Snippet:

.faquestion {
    cursor: pointer;
    color: #fff;
    border-left: 12px solid rgb(3, 59, 76);
    border: 2px solid #fff9e6;
    background-color: rgb(3, 59, 76);
    border-radius: 4px;
    padding: 10px 10px 10px 15px;
    vertical-align: bottom;
}

.faquestion:hover {
    color: #000;
    border: 2px solid rgb(0, 87, 152);
    background-color: #ddcba4;
}

.faquestion::before {
    content: attr(before-content);
    padding-right: 8px;
    font-weight: bold;
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
    $(document).ready(function () {
        $(".faquestion").attr("before-content", " + ");
    });
    function toggler(element) {
        $(element).next().toggle();
        $(element).attr("before-content", function (index, attr) {
            return attr == " +" ? " - " : " + ";
        });
    }
</script>
<p class="faquestion" onclick="toggler(this);"><strong>How do I go to there?</strong></p>
<p class="" id="answer1" style="padding-left: 30px; display: none;">Follow the yellow brick road.</p>

<p class="faquestion" onclick="toggler(this);"><strong>What is a virtual experience?</strong></p>
<p class="" id="answer2" style="padding-left: 30px; display: none;">The Virtual experiences is...</p>

The alternative solution presents a more concise method compared to the first one. It suggests using a class.

You'll need a rule containing a - symbol for your content: ''. Incorporate this into your CSS:

.faquestion.minus::before {
    content: " \002D ";
}

Introduce a class toggle mechanism using the toggleClass() method. Like so:

$(element).toggleClass("minus");

Snippet:

.faquestion {
    cursor: pointer;
    color: #fff;
    border-left: 12px solid rgb(3, 59, 76);
    border: 2px solid #fff9e6;
    background-color: rgb(3, 59, 76);
    border-radius: 4px;
    padding: 10px 10px 10px 15px;
    vertical-align: bottom;
}

.faquestion:hover {
    color: #000;
    border: 2px solid rgb(0, 87, 152);
    background-color: #ddcba4;
}

.faquestion::before {
    content: " \ff0b ";
    padding-right: 8px;
    font-weight: bold;
}

.faquestion.minus::before {
    content: " \002D ";
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
    function toggler(element) {
        $(element).next().toggle();
        $(element).toggleClass("minus");
    }
</script>
<p class="faquestion" onclick="toggler(this);"><strong>How do I go to there?</strong></p>
<p class="" id="answer1" style="padding-left: 30px; display: none;">Follow the yellow brick road.</p>

<p class="faquestion" onclick="toggler(this);"><strong>What is a virtual experience?</strong></p>
<p class="" id="answer2" style="padding-left: 30px; display: none;">The Virtual experiences is...</p>

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

SEO Friendly URL for JQuery Tabbed Content

I have developed a jQuery powered tabbed content system, where clicking on a tab changes the content below. However, I am facing an SEO issue. The SEO specialist has advised me to make it more SEO-friendly by adding different URLs to each tab. Can anyone ...

What is the process for establishing Many-to-Many connections with personalized field titles using Bookshelf.js?

I am interested in setting up a many-to-many relationship using Bookshelf.js and I would like to customize the names for the foreign key columns. Additionally, I want to have access to the helper table in Bookshelf just like in the example below: var Phys ...

Analyzing registration details stored in an array against user login credentials

With two buttons available - one for sign up and the other for log in, my goal is to gather input form values from the sign-up section into an array. Following this, I aim to compare the user input from the sign-up with that of the log-in, informing the us ...

implement a click event handler for GLmol (webGL)

Check out GLmol, a Molecular Viewer built on WebGL and Javascript by visiting You can see a demo of GLmol in action at I am interested in adding a click function to GLmol. For example, when I click on a ball, I would like to retrieve information about it ...

Integrating Excel into a webpage - is it possible?

Currently facing an issue on my website. I'm trying to open a 'file://' URL directly with the <a href=""> element in a browser, but it's prohibited. I'm searching for a plugin or similar solution that can enable me to execut ...

Angular popup window can't access DOM elements

Hey there, currently experimenting with an angular modal instance and running into a bit of a hurdle. When attempting to declare a querySelector in the modal controller, I'm encountering a console error stating 'Cannot read property 'queryS ...

`On mouseup event, changing specific text`

I've been working on a real-time HTML highlighter that surrounds selected text with span elements containing a background property. Check out the fiddle here: https://jsfiddle.net/4hd2vrex/ The issue arises when users make multiple selections, leadi ...

Scrolling container embedded within a parent with absolute positioning

I have encountered a tricky situation with an absolute positioned div and its nested div having overflowing content. My goal is to enable vertical scrolling for the child div, however, my attempts so far have been unsuccessful. The challenge here is that I ...

Is there a way to determine the negative horizontal shift of text within an HTML input element when it exceeds the horizontal boundaries?

On my website, I have a standard HTML input field for text input. To track the horizontal position of a specific word continuously, I have implemented a method using an invisible <span> element to display the content of the input field and then utili ...

`There was an issue with an unfinished string literal.`

Currently, I am utilizing jQuery to display the information from a JSON string generated by PHP and extracted from a database. However, I have encountered an issue where some of the data spans multiple lines... How can I prevent this from triggering an un ...

Repeatedly utilizing a drop-down menu

Can a <select> menu be written and utilized in multiple locations on a webpage? For instance: <select id="dm"> <option value="">Select Quantity</option> <option value="less ">50 - 60</option> <option value="me ...

Unable to properly delete data in Express/Postgres

After developing a Kanban board using JavaScript without any frameworks on the front end, I integrated Express/Postgres on the back end. However, I am encountering an issue with the 'Delete' operation. While all other CRUD operations are functio ...

Describe the CSS that targets pseudo-elements on an individual element

Perhaps a Repetition: CSS Pseudo-classes with inline styles Could an example like this work... <span style="this:hover{color:blue}">Changes color to blue when hovered</span> ...be doable? ...

Clicking on the image "Nav" will load the div into the designated target and set its display to none. The div will

Can someone help me with loading a div into a target from an onclick using image navigation? I also need to hide the inactive divs, ensuring that only the 1st div is initially loaded when the page loads. I've tried searching for a solution but haven&a ...

Combining `.then` promises in axios - what's the best approach?

Imagine having a wrapper function for the axios function - something that needs to be used in every ajax query to keep the code DRY. Here is an example: import axios from "axios" import NProgress from "nprogress" const query = (url, options) => { ...

Adjust the position of a div when the div above it expands

Text is displayed with a toggle button for icons. Beneath the text, there's a pagination div with a top margin set to position it correctly below the text. However, even though it follows the text in the HTML, it appears at the left corner of the main ...

Obtain the total number of result entries

I'm working on a project involving JS and PHP. My goal is to call a PHP file using Ajax and have it return the count of result lines. I use echo for this: $connection = new PDO($source, $user); $query = "SELECT * FROM scores WHERE username = '" ...

Sending file streams from the frontend to the backend service using Express.js and the Fetch

In my ExpressJS application, I have created an endpoint that needs to proxy a large raw binary to a remote backend REST service without storing anything in memory. To achieve this, I initially looked into using the request module with code like: req.pipe( ...

Enhancing Jquery performance by optimizing the .html() function for all elements within a specific class

My JQuery script performs well in Chrome and Safari, but struggles in Firefox. Let's not even discuss Internet Explorer... Whenever I run a method involving DOM manipulation, the page turns white for a second while processing. The issue seems to be w ...

Events triggered when the mouse hovers over an element and then moves

After spending hours reading articles and watching YouTube videos, I am completely lost. No matter what code I try, it seems like I can never get it right. It's frustrating how such a simple task can be so confusing. I just can't seem to wrap my ...