Changing the background-color with CSS class toggling

I'm having an issue with an element that is supposed to change its background color from "lightGreen" to "red" upon hovering. However, even though the class is being added properly, the color remains lightGreen.

It's worth noting that the element starts with the "disabled" class by default, but in this case, I am removing it before adding the "uiHighlight" class.

Why isn't it working as expected for me?

The CSS code looks like this:

#increaseImpulse, #decreaseImpulse, #undoLastAction {
    border: 1px solid black;
    background-color: lightGreen;
}

.uiHighlight {
    background-color: red;
}

.disabled {
     display: none;
}

And the HTML code:

<tr id="undoLastAction" class="disabled">
    <td colspan=2>
        Undo last action
    </td>
</tr>

As well as the JavaScript code:

$("#undoLastAction")
.hover(
    function(e){
        $(this).addClass("uiHighlight");
    },
    function(e){
        $(this).removeClass("uiHighlight");
    }
)
.click(function(e){
    console.log("undoLastAction")
});

Answer №1

Who needs jQuery when you can achieve the same result using only CSS:

#undoLastAction {
    background-color: blue;
}

#undoLastAction:hover {
    background-color: orange;        
}

Take a look at this jsFiddle example.

Answer №2

Using an id selector is more specific than a class selector, which explains why it wasn't working for you. You can try the following:

#undoLastAction.uiHighlight {
  background-color: red;
}

Hope this helps!

$("#undoLastAction").hover(
    function(e) {
      $(this).addClass("uiHighlight");
    },
    function(e) {
      $(this).removeClass("uiHighlight");
    }
).click(function(e) {
    console.log("undoLastAction")
});
#increaseImpulse,
#decreaseImpulse,
#undoLastAction {
  border: 1px solid black;
  background-color: lightGreen;
}
#undoLastAction.uiHighlight {
  background-color: red;
}
.disabled {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr id="undoLastAction">
  <td colspan=2>
    Undo last action
  </td>
</tr>
</table>

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

What are some ways I can customize the appearance of this Google Maps infoWindow?

I was able to create a Google Maps script using JavaScript code. The map displays multiple locations with corresponding latitude and longitude coordinates. This script can be viewed at . My objective now is to customize the appearance of the info windows ...

When the virtual keyboard is opened on the Nexus Player using an Android Cordova app, the first character is automatically typed

While my question may be specific to a particular device, I am seeking insights on how to prevent a common issue from occurring in general. My ReactJS app has a build for Android using Cordova, with one of the supported devices being the Nexus Player. The ...

Is there a way to track the sources from which my web page is being accessed or embedded?

Is there a way to modify the styling on my webpage depending on whether it is being accessed from one website where it's embedded or another website (as an iframe)? I've researched uri, requests, httprequests, and using "object sender," but have ...

Creating dynamic grid data based on various filter criteria using JavaScript (AngularJS)

In my approach, I have created two JSON data files. If the 'List' value equals A-1, I will retrieve the JSON data specific to that value. Otherwise, I will retrieve all the main data from data.json. My goal is to have 3 other checkbox options un ...

Exploring the world of nested selectors in conjunction with Vue.js CSS modules

Can nested css selectors be used with Vue.js css modules? For example, I am trying to scope this css (to avoid affecting child components): .list { ... .item { ... } } The documentation only provides examples without nesting, but is ...

I'm running into an InvalidSelectorError and I could use some assistance in properly defining

As I gaze upon a massive dom tree, my task using NodeJS/Selenium is to locate an element by the title attribute within an anchor tag and then click on the associated href. Despite being a newcomer to regex, I am encountering numerous errors already. Below ...

Converting objects into CSV format and exporting them using JavaScript

When exporting to CSV, large strings are causing other cells to be rewritten. See the screenshot for the result of the export here. For the code related to this issue, refer to this link. The question is how to prevent large string values in a cell from a ...

What is the comparison between actual pixels and text size in Internet Explorer?

Can you determine the actual font size in pixels corresponding to the following text size options in Internet Explorer? Largest Larger Medium Smaller Smallest In a web development project, I am looking to achieve a similar functionality to adjust the te ...

What is the best way to insert margin guidelines within a DIV being utilized as a WYSIWYG editor?

My latest project involves a design editor created using HTML and jQuery, allowing users to add text boxes, images, and more to build up unique designs. The canvas is represented by a bordered div where users can add elements by clicking on buttons that cr ...

Tips for looping through nested JSON data in Google Sheets

In my attempt to iterate through the nested "line_items" element in a JSON response triggered by a post event, I aim to populate a Google Sheets spreadsheet using Google Apps Script. My objective is to write the elements within each "line_item" into new ro ...

The WebView component is unable to display internally stored CSS files within the project

Looking to display HTML content with CSS styling stored in a file downloaded from a URL and saved within the "context.filesDir". The downloaded folder contains all the assets required by the CSS file. Despite passing the CSS file path to the webv ...

The formControl value is not being shown on the display

I am facing an issue with a form that has multiple fields created using formGroup and formControlName. The challenge arises when I launch the application and need to retrieve data from the back end to display it on the view. The specific problem I'm ...

Has anyone figured out the issue with Uplodify? It suddenly ceased to function

For some time now, I've been using uplodify without any issues. Unfortunately, as of yesterday, it suddenly stopped working. I'm at a loss as to what might have caused this sudden change. Could someone please offer me some assistance? I've ...

What is the best way to incorporate a <c:forEach> loop into JSP operations?

I am attempting to calculate the product of two variables that are associated with an item in a loop. Below is the code snippet from the JSP file: <table> <thead> <tr> <th>PRICE</th> <th ...

What is the most effective method for pausing execution until a variable is assigned a value?

I need a more efficient method to check if a variable has been set in my Angular application so that I don't have to repeatedly check its status. Currently, I have a ProductService that loads all products into a variable when the user first visits the ...

Steps to fix the Error: connect EISCONN ::1:5555 - Local (:ffff.127.0.0.1:5555)

Currently, I am in the process of developing an Electron app where I need to establish a connection with a TCP port on my local machine. However, upon starting the application, I encounter the following error message: "A JavaScript error occurred in the ma ...

Retrieving Precise Information from jQuery/AJAX and PHP Script

After encountering some challenges with my previous post on Stack Overflow being considered "too broad," I have decided to break down my questions into more specific inquiries. In this post, I am seeking guidance on retrieving precise data from a MySQL dat ...

What steps can be taken to address a TypeScript error when a function's parameters may be of two different types?

I'm facing an issue with a simple function that retrieves the address as a string interface AddressType1 { city: string | null; state: string | null; postalCode: string | null; } interface AddressType2 { city: string | null; region: strin ...

What is the most effective way to integrate webkitSpeechRecognition into a Vue.js project?

I found a demo at this link, but I'm having trouble understanding it. Can you provide a simple implementation? <div id="app"> <v-app id="inspire"> <v-container fluid> <v-layout row wrap justify-cen ...

Error message encountered in Rails Webpacker: "Uncaught TypeError: $(...).tooltip is not recognized as a function

I am working on a Rails 6 application where I compile assets using Webpack 4.39.1 with the help of the Webpacker gem. In my webpack configuration file (config/webpack/environment.js), I have included JQuery 3.4.1 and Popper.js as part of the ProvidePlugin ...