After my data rows are filled, I add jQuery and CSS striping animations to enhance their appearance

Before any click, they have this appearance:

Clicking on Sales in the business section reveals a drop-down select box. If I click on "Sales" and modify the text area, it will transition to grey. While the grey rows change accordingly, the black rows should fade to black, not grey.

After making a selection:

I changed Sales to property, and after applying the animate() function, it turned grey and disrupted the striping pattern.

This is my function:


success: function (result) {
if (result) {
//Update the verbiage of the ".look" element to match the new value from the ".touch" element
theLook.text(newDes);

//Switch out the elements so that it resembles a normal table cell again
back_to_look();

//Briefly highlight the span within it so that the user knows the modification was successful
theTd.css('background-color','#59b4d4').animate({ backgroundColor: "#333333" }, 1500);
} else {
//Flash the ".touch" element in red without reverting back to the ".look" element as there was an issue with updating the data
var oldColor = theTouch.css('background-color');
theTouch.css('background-color','#ff0000').animate({ backgroundColor: oldColor }, 1500);

How can I retrieve the original background color setting and restore it? Do I need to specifically target the CSS using jQuery's css() method?

Answer №1

Here is a better way to handle the background color change:

theTd.css('background-color','#59b4d4').animate({ backgroundColor: "#333333" }, 1500);

This code snippet changes the background color back to #333333 after the flash effect. To improve this, try using the following code:

var currentColor = theTd.css('backgroundColor');
theTd.css('background-color','#59b4d4').animate({ backgroundColor: currentColor }, 1500);

Instead of directly assigning the backgroundColor: #333333 in the animate function, we create a variable called currentColor to store the current background color of the table cell being edited.

By doing this, we ensure that the table cell reverts to its original color once editing is complete. This modification ensures flexibility and maintains consistency with the original background color. In the updated code, we set the background color to the currentColor variable instead of specifying it as #333333.

Answer №2

If you want to find out the background color, you can simply use this code:

$(document).on('click', 'div', function () {
    var x = $(this).css('background-color');
    hexColor(x);
    alert(color);
});

function hexColor(colorValue) {
    var parts = colorValue.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    delete(parts[0]);
    for (var i = 1; i <= 3; ++i) {
        parts[i] = parseInt(parts[i], 10).toString(16);
        if (parts[i].length == 1) parts[i] = '0' + parts[i];
    }
    color = '#' + parts.join('');
}

Check out an example online

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

Is there a way to eliminate the default arrow on a list input and replace it with a custom arrow solely with html and css?

Is there anyone who can help me figure out how to remove the default arrow in the list input field and replace it with a new arrow icon? <body> <form > <label for="data-list">Choose a browser from this list &l ...

Adjust the properties within the component's styles using Angular 2

In this project, the objective is to dynamically change the background-color based on different routes. The goal is to display a specific color for UpcomingComponent while keeping the background-color consistent for all other routes. The approach involves ...

Sending form information through AjaxPassing information from a form using

Currently, I am working on a project where one page opens a thickbox of another page that contains a form. Once the form is submitted and data is written to the database, I need the parent page of the thickbox to update specific rows of the form that have ...

The functions Show() and Hide() may not work in all scenarios within jQuery

I'm currently developing a website that allows users to participate in quizzes. Each quiz consists of 20 questions divided into three sections: 1 mark for 10 questions, 2 marks for 5 questions, and 4 marks for 5 questions. For each question, there are ...

What are some techniques to ensure a bookmark retains its original style even when the cursor hovers over it?

My CSS skills are limited, so I am facing what may seem like a basic issue. On my website, I have a table of contents with links structured like this: <a href="#user-interface">User interface</a> Additionally, there is a bookmark in another ...

Staggered Drop-Down Menus

I've created a custom CSS drop-down menu that works well, but I've realized that updating this menu across all the HTML pages on my website would be a tedious task. If I ever need to add a new section and tier to the menu, I'd have to update ...

Pass data between JavaScript and PHP using the Phaser framework

I am trying to pass a JavaScript variable to PHP and then store it in a database. Despite searching for solutions on Google, I have not been successful. Most suggestions involve using AJAX, but the code doesn't seem to work when I try it. I attempted ...

I am encountering an issue where my input is moving to the following line after the initial one. What could be

Trying to create a layout I like for this form has been a bit of a struggle. I thought I had something good at first, but after the first row of input, the formatting gets all messed up and I can't figure out what's going wrong. <!DOCTYPE htm ...

Using jQuery and Ajax to fade in content after all images and other assets have finished loading

I've encountered an issue with loading pages via ajax for users with custom URLs. For example, a profile is usually found at http://example.com/users/Dan, but if a user has a custom URL like http://example.com/DansCustomURL, I need to fetch their desi ...

"Choosing the text within an <a> tag: A step-by

I am dealing with the a tag. <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b4c7dbd9d1f4d9d5ddd89ad7dbd9">[email protected]</a>"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" ...

When you click on an anchor tag, the divs do not automatically move to the top of the page

Encountering an issue with a rather straightforward website. It consists of a vertical scroll with a left side navigation. Whenever I click on a menu item, it fails to bring the corresponding section to the top of the page. I've experimented with a fe ...

Does the jQuery selector match an unset class?

Encountering an issue with a classed based switch in jQuery code, not sure if it's my mistake or a bug. Take a look at this example: // Expecting only one run // Instead it keeps triggering $('.switch').click(function () { // Show mess ...

What causes the off-canvas menu to displace my fixed div when it is opened?

Using the Pushy off-canvas menu from GitHub for my website has been great, but it's causing some trouble with my fixed header. When I scroll down the page, the header sticks perfectly to the top, but once I open the off-canvas menu, the header disappe ...

The .prepend() method receives the variable returned by ajax and adds it

I'm facing a challenge with adding a dynamic select box to a string within my .prepend() function. The options in the select box are subject to change, so hard coding them is not an option. To tackle this issue, I am using an AJAX call to construct th ...

Creating a custom fav icon within a button with CSS styling

https://example.com/code-example Hey there, I'm attempting to replicate the button showcased in the code example above. However, I'm facing a challenge when trying to incorporate the stars without altering the existing script. Another issue is w ...

Selecting content loaded via Ajax using jQuery

I recently implemented a library that I found at this website According to the documentation, I need to use the following syntax: autosize($('textarea#description')); This works fine for regular elements, but when my textarea is loaded via aja ...

Vue.js - dynamically applying CSS classes based on conditions

Within a VueNative project that utilizes NativeBase as a component library, with tags prefixed by nb-, the code snippet below is found within the <template> tags of the footer.vue file which houses navigation buttons. This piece of logic successfully ...

Issue with Mobile Touch Screen Preventing Vertical Scrolling

Currently experiencing difficulties with a div element that is not allowing touch and vertical scroll on mobile devices. Although scrolling works fine with the mouse wheel or arrow keys, it does not respond to touch. Have tested this on various devices and ...

Troubleshooting unexpected character error in jQuery Ajax call with ColdFusion 9: JSON parsing issue

I'm brand new to working with AJAX so please bear with me. I'm currently trying to retrieve data from a database using a cfc component with <cfquery> and then converting it to JSON using the serializeJSON(var) function. However, when I che ...

Dynamic Select Box with Display of 2 Buttons

Looking to create an Ajax combo box with options for Female and Male. I would like to display a button for Female and a button for Male. Here is the code that I have: <!DOCTYPE html> <html> <head> <style> #for-male, #for-female{ ...