Changing the background color of a table using jQuery toggle

I'm currently working on creating an interactive menu using jQuery for some hands-on practice. At the moment, I have set up my dropdown menu and a side table with a few elements.

https://i.sstatic.net/IZIbL.png

The dropdown menu consists of 4 elements. My objective is this: when hovering over the first element in the dropdown menu, the first cell in the table changes to red, just like shown in the accompanying image. Similarly, when hovering over the second element from the dropdown list, the second cell (vertically) in the table should turn red while the first one returns to its original color.

Currently, I've managed to change the color of the first cell when hovering over the corresponding element in the dropdown. However, the issue lies in it not reverting back to its original color afterward. Here's the code snippet I used:

$(".first-hover").hover(function(){
    $('#first').css({'backgroundColor':'red'})
});

Appreciate any assistance you can provide!

Answer №1

The jQuery method .hover() is designed to handle two functions: one for when the mouse is over an element and another for when it moves out.

$(".hover-effect").hover(function(){
    // Perform actions on mouse over
    $('#element').css({'backgroundColor':'blue'})
}, function(){
    // Perform actions on mouse out
    $('#element').removeAttr('style');
});

Answer №2

Here's a simple way to switch the color back to its original shade. Assign a class to your menu items.

.greenclass{
  background-color:green;
}

 .purpleclass{
   background-color:purple;
  }

 $(".hover").hover(function(){

    if($(".hover").hasClass('greenclass'))

  {
     $(".hover").removeClass('greenclass')

     $(".hover").addClass('purpleclass')
  }

   $(this).addClass('greenclass')

});`

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 utilizing console log within a react form component

I'm currently exploring ways to communicate with a React form in order to provide it with an object.id for updating purposes. While I can successfully console log the object.id within the update button and modal, I am struggling to confirm if the val ...

Interactive front end design for decision trees

On the front end, I aim to enable users to influence the outcome of a Decision Tree based on their selections. For my Django-React App, I have adopted the style and tree example from the codeplayer. You can find it here: I am tasked with creating an unor ...

Utilizing the getJSON function in jQuery in conjunction with an ASP.NET Web Form

How can I execute a method on an ASP.NET Web Form page by using the getJSON method in jQuery? Here is the objective: User selects an item from a list The selected value is sent to the server Server responds with a related list of data formatted in JSON ...

Steps for assigning a URI as a variable based on the environment, whether it is in production or not

Seeking advice on deploying a MERN app onto Heroku. I have the mongodb URI declared in a config file locally, but on Heroku I am using process.env.mongoURI. How can I adjust my code to use the local config file when running locally and the Heroku config wh ...

A guide to efficiently reusing parameter definitions in functions using JSDoc

Currently, I have HTTP handlers set up in my express application. I want to use JSDoc annotations to document these handlers in a reusable manner. Here is what I currently have: /** * * @param {functions.Request} req * @param {functions.Response} res ...

Using radio buttons to generate a URL based on the user's selection

I currently have a set of 3 Radio Buttons along with a Select button - my goal is to dynamically change the select button based on which radio button is chosen. Here's an example of what the HTML code looks like: <INPUT TYPE="radio" id="Orange"&g ...

JavaScript: utilizing JSON, implementing dynamic methods for creatures, and utilizing closures for encaps

Apologies for the unclear title, but I am unsure where the issue lies. Therefore, my task is to create a function that generates JavaScript objects from JSON and for fields that start with an underscore, it should create setters and getters. Here is an e ...

Troubleshooting: Why isn't the Access-Control-Allow-Origin header functioning correctly?

I'm currently trying to address the HTTP OPTIONS method by including an Access-Control-Allow-Origin header that mirrors the information in the Origin header from the request. Unfortunately, it seems like this approach isn't functioning properly, ...

Determine the sequence of a div based on its class

Here is the code snippet I am working with: <div class="test"></div> <div class="test"></div> <div class="test"></div> <input type="button" class="button" value="get number"> <div class="test"></div> & ...

add component automatically upon selection

Imagine I have a special <SelectPicker/> element that allows me to choose an option. What I am trying to figure out is how I can include another <SelectPicker/> once I have made a selection. function DynamicComponent() { const [state, setSta ...

Check if the input values are already in the array and if not, then add

Within my React application, I am displaying an Array and each entry in the Array is accompanied by an input element. These input elements are assigned a name based on the entry's ID, allowing users to enter values. To handle the changes in these inp ...

How to compare two enums in TypeScript using their string values?

I am working with 2 enums: enum Insurer { PREMERA = 'premera_blue_cross', UHC = 'united_health_care' } enum ProductSource { PremeraBlueCross = 'premera_blue_cross', UnitedHealthCare = 'united_health_care' } ...

Retrieve the parent element of the selected tag within an iframe

I have an embed iframe that displays an HTML page. Now I am trying to retrieve the class of the parent item that was clicked. <iframe src="//example.com" id="frame" ></iframe> This is the CSS styling for the iframe: #frame{ width:380px; he ...

Angular: An excessively large number of dynamically generated FormGroups within a FormArray is causing the webpage to become unresponsive

In my project, I have implemented a dynamic FormArray generation process which is triggered by the following code snippet. this.priceListForm.addControl('priceListLines', this.formBuilder.array(this.priceListDetails.priceListLines.map(item => ...

When the Json payload is lengthy, an Ajax request to an ASP.NET MVC Controller results in a 404 error

I am facing an issue with my ajax call that involves passing a json string to a controller action. When the content portion of the json is too long, or when the json string in general exceeds a certain length, the server returns a 404 error. However, if I ...

"Using the Google Maps directive inside a separate modal directive in Angular results in a blank map display

As a newcomer to Angular, I have encountered a hurdle while attempting to incorporate a 'google maps' directive inside another directive. The following code showcases a 'modal-view' directive that loads a form: angular.module(&apo ...

The Node server is unable to retrieve the compiled React code

My react codes have been built and are saved in the "/build" directory. Below is my node code located at /server/index.js: import dotenv from 'dotenv'; import Express from 'express'; import http from 'http'; import path from ...

Simulate a keyboard key being pressed and held for 5 seconds upon loading the page

Is it possible to create a script that automatically triggers an event to press and hold down the Space key for 5 seconds upon page load, without any user interaction? After the 5 seconds, the key should be released. It is important to emphasize that abso ...

Retrieving JSON data using Jquery (undefined)

When attempting to retrieve a value from JSON data, I am receiving 'undefined'. $.get( baseURL + 'vacation/show/' + name_surname, function( data ) { alert(data); // returns [{"id":"1","title":"Testas","start":"2015-03-04","end":"20 ...

Prepare yourself for the possibility of receiving a caution while within a try-catch block

After implementing the MongoClient.connect method and encountering the warning 'await' has no effect on the type of this expression, it became clear that including the .catch line immediately following (which is currently commented out) mitigated ...