Including 'active' in an HTML button does not trigger the styles specified in the :active pseudo-class

When a button is clicked, I want its color to change.

Below are the HTML elements in question:

<div class='chatbot-container'>
      <div class='chatbot-wrapper' id='chatbot-wrapper' style="display:none;" >
        <iframe
          id="chatbot"
          class="chatbot"
          allow="microphone;"
          src="https://console.dialogflow.com/api-client/demo/embedded/xxx-xxx-xxx"></iframe>
        </iframe>
      </div>
      <button class='chat-btn' type="button" onclick="toggleChatbot()">Q</button>
    </div>

Here is the CSS stylesheet:

.chat-btn {
    background: red;
    color: #fff;
    font-size: 32px;
    ....
    <code snipped for brevity>
    ...
}
.chat-btn:hover {
    background: #fff;
    color: red;
}
.chat-btn:active {
    background: #fff;
    color: red;
}

And here is the JavaScript code:

<script>
      function toggleChatbot (chatBtn) {
        var x = document.getElementById("chatbot-wrapper");
        if (x.style.display === "none") {
          x.style.display = "block";
          chatBtn.className += ' active';
          console.log(chatBtn.active)
        } else {
          x.style.display = "none";
          chatBtn.className = chatBtn.className.replace(" active", "");
          console.log(chatBtn.active)
        }
      }
    </script>

Although the 'active' class is added as expected, the CSS pseudo-class styles are not being applied. You can view a snapshot here.

Answer №1

It appears that adding a pseudo class to an element using JavaScript is not possible, as it is not a real class. However, you can create additional classes like chat-btn-hover that mimic the behavior of pseudo classes!

Check out these resources for more information:

Selecting and manipulating CSS pseudo-elements such as ::before and ::after using jQuery

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 adjusting height responsively with Bootstrap 4

Utilizing bootstrap to create div elements. Check out the fiddle link below: fiddle On my page, I have 4 divs - two on top and two at the bottom. The height of all 4 card divs should remain the same and not change. In the last div, there could be on ...

Unable to access the attributes of the mongoose model

I'm experiencing an issue with my mongoose model.find code below Displayed here is my node.js code that utilizes mongoose to interact with MongoDB. However, upon running it, I encounter the following error message: Starting... (node:7863) Unhandl ...

Troubleshooting a LESS compiling issue with my Jade layout in ExpressJS

Implementing LESS compilation on the server side using Express was successful, but I faced an issue with jade not recognizing less in layout. Error message displayed in my terminal: if(err) throw err; ^ Error: ENOENT, open '/Users/li ...

Set the display property of all child elements within the DIV to none

CSS <div class="container"> <span></span> <input type="text"> </div> JavaScript function hideElements(){ let container = document.querySelector(".container"); let elements = ...

issues with the styling and scripts within the jsp document

During my project work, I encountered an issue with the front end as shown in the image below. https://i.sstatic.net/gLIUr.png Upon loading the project, all styles and scripts disappear completely. How can I resolve this issue promptly? I attempted to up ...

The Bootstrap 4 modal appears to be failing to load properly, as it is only

Trying to trigger a Bootstrap modal with a button click. Currently, there is one functioning modal on the page that works fine. However, when attempting to load another modal on a button click, only a faded screen appears. <button class="btn btn-green ...

Troubleshooting Problems with React-Scroll Link Element in Navigation Bar

Having trouble implementing a hover effect on my navigation links using the Link component from the react-scroll library. The desired effect is for the links to increase in size when hovered over by using transform: scale(1.1). Unfortunately, the effect is ...

How should we structure our JavaScript code: MVC or self-rendering components?

I'm in the process of developing a highly JS-centric web application. The bulk of the work is being carried out on the client side, with occasional syncing to the server using AJAX and XMPP. This is my first venture into creating something of this ma ...

"Experience the stunning transformation of 3D objects from poor rendering to exceptional quality in the GLTF-Files Online

I am looking to showcase a model created in Blender on a webpage. The issue is that the rendered quality of the model seems poor, especially with the edges not being properly displayed. Interestingly, when viewing the model in an online viewer, the edges a ...

Getting the local path of a file from an input file in Angular 7

Is there a way to retrieve the local file path from an input field in HTML? After running the code below, I obtained 'C:\fakepath\fileTest.txt' I am looking for a method to get the local path so that I can pass it on to my control ...

Apply specific margins to every second element in media queries using the margins that were previously set for every third element

Looking to adjust the margin for every second element instead of every third when using media queries. .title:nth-child(3n+3) {margin-right:0 !important} @media screen and (max-width:1160px) and (min-width:900px){ .title:nth-child(2n+2) {margin-right:0 ! ...

What is the process for creating a button that can sort an array and count its elements?

I am having trouble creating a code that can sort products into alphabetical order and then count the number of items in the list within the same program. I have managed to write separate programs that achieve each of these tasks individually, but when I ...

Sequelizejs establishes a belongsToMany relation with a specified otherKey

I am currently developing an app centered around songs and artists, and here is the database schema I have designed: Each Song can have multiple Artists associated with it, and each Artist can be linked to several Songs as well. This establishes a many-to ...

Is it possible to eliminate the css class prefix when using Modular css in React?

When working with React & NextJS, I have observed that my modular SCSS files are automatically prefixing my classnames with the name of the file. Is there a way to turn off this feature as it is making the DOM structure difficult to interpret? For instanc ...

Verification of radio selections

I have successfully implemented validation for the radio button. Now, I would like to add an alert box that asks "Are you sure you want to pick 'x'?" before proceeding with the code. If the user clicks cancel, they should return to the webpage. B ...

What is the best method for including parameters in OBJECT_URL when sharing a post on Facebook?

Regarding this line: FB.api('/me/namespace:read' + '?article=http://www.xxxxxxxxxxxxxx/tm/redir.php&access_token=','post', Whenever I attempt: I encounter errors. How can I successfully pass parameters in the OBJECT_UR ...

What is the best way to adjust the margin in a fluid container?

Why is there no margin on the right side of the box while we can apply margin on the left side of the container? It seems that the intention is to cover the entire horizontal width of the screen. <!DOCTYPE html> <html lang="en"> <head> ...

Tips for aligning two divs on top of each other

I'm trying to figure out how to have divs start drawing from the same point. Essentially, I want to be able to add new divs and have them appear stacked on top of each other, so that they can all move together based on a single reference point. Here& ...

Unable to locate phonepe.intentsdk.android.release:IntentSDK:2.4.1 while integrating React Native

android build gradle : // Configuration options for all sub-projects/modules are defined in the top-level build file. buildscript { ext { buildToolsVersion = "33.0.0" minSdkVersion = 21 compileSdkVersion = 33 targetSdkVersion = ...

Is there a way to access an SD card by clicking on an HTML link using JavaScript?

UPDATE: I am seeking a way to embed an HTML file with JavaScript or jQuery that can directly access the contents of the SD card while being opened in a browser. Currently, I have posted code for accessing it through an activity, but I want to be able to d ...