Switch the color of an element when onmouseout occurs

I am currently working on a C# web application that includes an asp:Calendar:

<asp:Calendar ID="cal_ReserveDate" runat="server"
                            DayStyle-ForeColor="DarkBlue" DayHeaderStyle-BackColor="#FEF6CB" DayStyle-Height="25" DayStyle-Font-Bold="true"
                            SelectedDayStyle-BackColor="#003F7D" SelectedDayStyle-ForeColor="White"
                            DayNameFormat="FirstLetter" ShowGridLines="true" BorderColor="Black"
                            TitleStyle-BackColor="#003F7D" TitleStyle-ForeColor="White" TitleStyle-CssClass="CalHeader"
                            NextPrevStyle-CssClass="CalNextPrev" NextPrevStyle-ForeColor="White"
                            OnVisibleMonthChanged="cal_ReserveDate_VisibleMonthChanged"
                            OnDayRender="cal_ReserveDate_DayRender" OnSelectionChanged="cal_ReserveDate_SelectionChanged"
                            DayStyle-BorderColor="Black" SelectedDayStyle-CssClass="CalendarSelectedDay" Width="97%" Font-Bold="true" />  

My goal is to customize specific date colors using the OnDayRender function, such as for closed or sold out dates. I also want to allow our design team to set today's color through the stylesheet. To start, I added this CSS style:

.calendarToday {
    background-color: mistyrose;   
}  

In my rendering code for "today," I have the following:

if (e.Day.IsToday)
{                
    string onmouseoutStyle = "this.style.backgroundColor='@BackColor'";    
    e.Cell.CssClass = "calendarToday";   
    e.Cell.Attributes.Add("onmouseout", onmouseoutStyle.Replace("@BackColor", <<read current bgColor>>));
}

This logic resembles what I use for marking sold-out days and resetting background colors upon mouse-out, but in other instances, I have fixed predefined colors hardcoded in the app. My challenge now is figuring out how to access the e.Cell attributes, particularly the background-color, to determine the color set by designers in the calendarToday style sheet.

If anyone can provide insight into accessing this information, it would be greatly appreciated! Thank you for your attention - Jim

Answer №1

It has become clear to me now. To restore the background-color to its CSS-specified value, you can simply set it to an empty string like so: this.style.backgroundColor = ''. This action will eliminate the inline style background-color on the cell.

Live Demonstration

var cells = document.querySelectorAll('td');

for (var i = 0, l = cells.length; i < l; i++) {
  cells[i].addEventListener('mouseenter', function() { this.style.backgroundColor = 'goldenrod'; });
  cells[i].addEventListener('mouseout', function() { this.style.backgroundColor = ''; });
}
.calendar {
  border-collapse: collapse;
}

.calendar td {
  padding: 10px;
  border: solid 2px black;
}

.calendarToday {
    background-color: mistyrose;   
}
<table class="calendar">
  <tbody>
    <tr>
      <td>Sunday</td>
      <td>Monday</td>
      <td>Tuesday</td>
      <td>Wednesday</td>
      <td class="calendarToday">Thursday</td>
      <td>Friday</td>
      <td>Saturday</td>
    </tr>
  </tbody>
</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

Having trouble transferring ASP server control value to a Javascript function

Currently working with ASP.NET 2.0. My Entry Form contains various ASP.NET Server Controls, such as TextBoxes. I want to pass the value of a TextBox on the onBlur event. Here is an example of a TextBox code: <asp:TextBox ID="txtTotalTw" Width="80px" r ...

Make sure your website design is responsive by setting the body to the device's

https://i.sstatic.net/3xHcx.png I'm struggling to make the body of my page responsive for mobile users. Whenever I try using this line of code, it just creates a messy layout. Any advice on how to fix this? <meta name="viewport" cont ...

Arrange list items in a circular pattern

Is there a way to achieve the desired appearance for my navbar like this link? I have tried rotating the text, but the vertical text ends up too far from the horizontal. Any suggestions? .vertical { float:right; writing-mode:tb-rl;/*IE*/ w ...

Using Bootstrap 4 beta for Form Validation

I'm struggling to grasp the concept of building validation forms. The bootstrap documentation includes a JS code snippet (referred to as starter code) like the one below: <script> // Example starter JavaScript for disabling form submissions if ...

Is there a way to dynamically toggle the visibility of a floating textarea between on and off?

I have my own blog website: Essentially, what I am aiming for is When a user clicks on the search button, a floating semi-transparent textarea window should appear inside the designated rectangle area (as shown in the image, that red orange rectangle). ...

Using Json.Net to deserialize intricate data structures

I am facing an issue with deserializing JSON for the following classes: public class Players:List<Player> { } public class Player { public Player() { PlayerTeam = new Team(); } public int PlayerId { get; set; } public T ...

What is the reason for the leftward shift of the buttons?

Currently following a Udemy tutorial on Node, React, and Express. The instructor is using a Header component in his code that shifts the Login and Cart links to the far right. I have implemented the same code as instructed, but my links are not aligning t ...

Navigate the conversation within the dialog without affecting the content below

Is it possible to have a dialog with scrollable content on top of a page that is also scrollable, but when trying to scroll inside the dialog using the mouse wheel, only the dialog body scrolls and not the page below it? What approach can be used to accom ...

MongoDB encountered a serialization depth error that exceeded the maximum allowed limit

I am relatively new to working with MongoDB and could use some help with a basic question. I am currently using the MongoDB C# driver in my ASP.net MVC project. However, I have encountered an error while trying to execute the MongoDB C# update operation. ...

What is the best way to reduce the size of an image using a URL?

As I work on creating a website in React for a company, my main focus is on developing a drive repository where users can upload files. One issue that has arisen is the performance of the "photos" folder, as it contains numerous high-resolution images th ...

Changing the border color of material ui Rating stars: A guide

I am struggling to change the border color of stars in Material UI Rating because my background color is black, making it difficult to see when the star is empty! Here's the code snippet: import Rating from '@material-ui/lab/Rating'; import ...

Forced Landscape Viewing on Mobile Site with No Auto-Rotation

My website is equipped with a mobile stylesheet: <link rel="stylesheet" href="css/mobile.css" media="handheld"> In addition to that, I am utilizing jQuery to customize functionality for mobile devices. I am wondering if there is a method to enforc ...

Python: parsing comments in a cascading style sheet document

I am currently working on extracting the first comment block from a CSS file. Specifically, I am looking for comments that follow this pattern: /* author : name uri : link etc */ and I want to exclude any other comments present in the file, such as: /* ...

Is it necessary to utilize Azure Media Services and Player for a solely audio-based application?

I'm interested in creating an ASP.NET application where users can upload audio files and listen to them on the site. I was thinking of utilizing Azure Blob Storage for storing the media, but I'm not sure if I need to also incorporate Azure Media ...

How to efficiently traverse an array in C# by making use of multiple iterations

We have a unique application that requires the deserialization of data from a single stream into various objects. The Data array contains multiple messages packed together, each with variable length and no specific delimiting codes in the stream. Our goa ...

Ever since I switched to using Angular4, my CSS has been acting up and no longer seems to be functioning properly

Ever since I switched to Angular 4, my CSS seems to have stopped working. While constructing the basic template for my app, I am encountering some challenges with CSS not being applied correctly to DIVS due to the generated Component DOM element of Angula ...

Display the validation error directly on the field as feedback from the AJAX calls

I am facing an issue with displaying error messages inline on the field when validation fails and is returned from an AJAX call. <input type="text" class="text-danger float-right" style="border:none;font-size: smaller&qu ...

Using jQuery to alter the colors of a dynamically generated table based on its content

I am currently working with a div element that dynamically generates a table. Although I can generate the table and use on("click" or "mouseover", function) to achieve certain results, I need the table to update as it loads. In my initial attempts, I tri ...

CSS: Deciphering the Hidden Message in this Code Snippet!

Update In my CSS file, I encountered a conflict with the following snippets. When I include this section: <style type="text/css"> ::selection{ background-color: #E13300; color: white; } ::moz-selection{ background-color: #E13300; colo ...

Deactivate the option for interactive logins

When deploying my application, I am looking to create a unique user profile. How can I disable interactive logon for this specific user in C# to prevent unauthorized access? In simpler terms, how do I properly set the SeDenyInteractiveLogonRight permissio ...