Restoring the background color to its original shade following alterations made by jQuery

In my table, I have multiple rows with alternating white and grey backgrounds achieved through CSS.

.profile tr:nth-child(odd)    { background:#eee; }
.profile tr:nth-child(even)    { background:none; }

However, I now want to allow users to select a row and have it highlighted in yellow using JQuery.

$(".Select").click(function() {
    //Deselect all other Rows
    $(".Select").show().prev().hide().parent().parent().css('background', 'none');

    //get Id
    var Row = $(this).parent().parent();
    var MatchId = Row.attr('id');
    $(this).hide().prev().show();
    Row.css('background', '#FFFFBA');
});

The issue arises when selecting all rows with the $(".select")....parent().parent().css('background....

Setting it to "none" turns all rows white and disrupts the alternating color scheme. Is there a way to restore the background property to its original state?

Answer №1

Introduce a new category:

.profile tr.hover    { background:#FFFFBA; }

instead of

 Row.css('background', '#FFFFBA');

you can opt for

  Row.addClass("hover");

and in place of

  $(".Select").s.//.parent().css('background', 'none');

you should go for

  $(".Select").s.//.parent().removeClass("hover");

The // is simply used to abbreviate the lengthy line.

Answer №2

Don't stress over it, simply utilize the CSS selector for the TR in your .css stylesheet and incorporate the :hover markup to let the browser take care of it. It's best to delegate tasks to the underlying browser whenever possible.

However, based on your code, it seems like you might actually want to change the background of a different row than the one you're currently on. In that case, you will need to add some .data() to the target object and trigger a mouse out event to revert it back to its original state.

Answer №3

Display the element with the class "Select", hide the previous element, and change the background color of its parent's parent element to transparent.

Answer №4

Caspar's explanation is spot on for a number of reasons:

  1. By keeping your presentation in the CSS, you maintain separation of concerns.
  2. Browsers are optimized to apply CSS styles more efficiently than manipulating the style object with JavaScript.
  3. If you want to add additional styles to the highlighted item, there will be no performance impact thanks to reason 2.

However, if you prefer to stick with your current code, you can achieve the desired result by excluding odd rows from your jQuery selector using the $.not() method:

$(".Select").show().prev().hide().parent().parent().not(':nth-child(odd)').css('background', 'none');

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

Using ReactJS to toggle a div upon clicking

I am facing an issue with toggling a div by clicking on text within a toggle bar. I am in the process of recreating a WordPress website template in React JS that was originally built using bootstrap. You can view the original template here. So far, I have ...

Position an anchor image at the top left corner, no matter the DTD or browser

Is there a way to keep an image with an href fixed in the top left corner of a webpage without affecting any other content and ensuring it functions consistently across different browsers and DTDs? I am facing the challenge of needing to provide a code bl ...

Instructions for inserting <tr></tr> after every fourth iteration of <td></td> in the loop

I am trying to create a list of twenty people with each tr containing 4 people, and I want to break from the loop after every fourth number. Each td value should be incremented in order. This is the code snippet I have been working on: <?php for ($i ...

Firebase hosting adjusts the transparency of an element to 1% whenever it detects the presence of CSS opacity

I'm currently working on a website using Vue.js and Firebase. Everything runs smoothly locally, including a button with a desired low opacity. However, when I deploy the site to Firebase Hosting, the button's opacity inexplicably changes to 1% an ...

Sending JSON data from SpringMVC to Java server

I am working with a javascript array that is structured like this: var citizens1 = [{"startLat":null,"startLng":null,"socialSecurityNumber":null}]; The array contains 500 records, and instead of sending each record individually to the server, I convert ...

Server unable to process header location following JS redirect, even though it functions correctly on localhost

I have been developing a login system that directs users to specific URLs based on their user type and activation status. Below is the code snippet: $.post( './assets/tasks/process-login.php', { email:email, ...

Develop a PHP Ajax jQuery UI Progressbar to track the progress of a lengthy MySQL task

My PHP script is taking a long time to run due to frequent SOAP calls to another website. I want to add a progress bar to track the completion percentage of this operation. I think the best approach is to use asynchronous Ajax calls to execute the PHP scr ...

The Safari 7.1+ browser seems to be having trouble with the spotlight effect

Why is the CodePen not functioning properly in Safari? Despite working well in Chrome and Firefox, it completely fails to work in Safari 7.1+. Can anyone provide some insights? http://codepen.io/cchambers/pen/Dyldj $(document).on("mousemove", function( ...

Update: "Mui V5 - Eliminate collapse/expand icons in TreeView and reduce TreeItem indentation"

My current project involves Mui V5 and I am looking to customize the TreeView component. Specifically, I need to remove the collapse/expand icons as I want them to be integrated into the TreeItem label component on the left side instead of the right. Add ...

Navigating rows in a Kendo Grid using buttons (Starting, Previous, Next, Ending)

Looking for help with navigating rows on a Kendo-Grid. I have four Buttons - First, Previous, Next, and Last. The goal is to highlight the selected record on the grid when a button is clicked. However, I'm struggling with making the Kendo-Grid highlig ...

A pair of buttons and text with varying alignment

Here is the code from my popup.htlm file: HTML <body> <select style="width: 100%;" multiple id="HLSlist"> </select> <span style="display:inline"> <button type="button" id="Deletebut">Delete</button> ...

Click here for a hyperlink with an underline that is the same width

As we work on our website, we want a unique feature where links are underlined when hovered over, with the underline staying the same width regardless of the length of the link text. How can we achieve this using CSS/jQuery/Javascript? ...

Utilizing Bootstrap CSS within Vue's scope

I'm attempting to integrate Bootstrap into a Vue component while ensuring that all CSS is scoped. My initial approach was as follows: <style scoped> @import "~bootstrap/dist/css/bootstrap.css"; @import "~bootstrap-vue/dist/bootstrap-vue.css"; & ...

Dynamic Content Sorting with JavaScript and jQuery

I am presenting various courses that are available, and I am utilizing JavaScript/jQuery to display or hide them based on element classes. You can view the page here. Currently, the script conceals all content on the page and then reveals items that matc ...

Using the responsive design mode may not accurately reflect the mobile user experience

Recently, I've been working on designing a new cover for my book using HTML and CSS. After completing the design, I previewed it in Responsive Design mode on both Firefox and Google Chrome. However, when I tried to view the website on my phone (a Gala ...

How tall can an image be to load successfully on an iPad website?

Similar Query: Max image width in Mobile Safari? Dealing with unwanted downscaling on panoramas What is the maximum height an image can be loaded at on a website when viewed on an iPad? I've tried loading an image (a sprite) with a height exceeding ...

Vue Labyrinthine Design theme

Looking for some guidance from experienced developers out there! I'm currently exploring how to incorporate Mazeletter as a background feature for each page in an app project I've been working on. This is all new territory for me, so any assista ...

Trouble with jQuery addClass function when trying to add to a specific object variable

I'm attempting to give an error class to an input textbox to inform the user that their input is invalid. Within the change event, I am referencing what seems to be the input field and storing it in a variable. However, calling addClass on the var ...

Efficiently making JQuery Ajax requests using HTTP Basic Authentication

I am currently experiencing challenges with implementing HTTP Basic Authentication in JQuery when communicating with a REST-based server. This server provides responses in both XML and JSON formats, but I have chosen to work with JSON format. Our authoriz ...

Tips for accessing hidden field values in a second jsp page

I have a webpage called page1.jsp where I am including hidden fields. Here is the code snippet: <form action = "page2.jsp" method = "post" id = "hiddenValuesForm"> <input type = "hidden" name = "userData" value="" id = "useDataID"> <input t ...