Using the nth-child pseudo selector on a particular row class for styling purposes

Can't seem to figure out if the issue I'm facing is related to my browser or CSS3.

I am using a datagrid with a basic HTML table structure:

<table>
    <thead>
        ...
    </thead>
    <tbody>
        <tr class="found">
            <td>...</td>
            <td>...</td>
            <td>...</td>
        </tr>
        <tr class="found">
            <td>...</td>
            <td>...</td>
            <td>...</td>
        </tr>
        <tr class="found">
            <td>...</td>
            <td>...</td>
            <td>...</td>
        </tr>
    </tbody>
</table>

A jQuery function is being used to search within the TD elements. If found, it maintains the "found" class. If not found, it removes the "found" class and adds a "not-found" class which sets display to none.

The search function is functioning correctly and classes are being assigned as expected. However, I am encountering an issue with applying styling to alternate rows using CSS pseudo selectors.

tr.found:nth-child(even) {
    background: #fff;
}
tr.found:nth-child(odd) {
    background: #000;
}

This styling works fine before a search is performed. But post-search, when the "not-found" class is applied, the pseudo selector seems to only apply to the element rather than the element along with the class. It appears that the pseudo selectors are set on page load and remain static thereafter.

I could manually reassign specific even and odd classes in my jQuery search function, but that would become messy. Additionally, since my column headers are sortable, I'd have to reapply these classes upon each sorting event, making the process inefficient. This repetitive class change might be visible as data samples increase, something I'm trying to avoid.

Any suggestions to resolve this issue?

EDIT

I've created a JSFiddle for you all to play around with: http://jsfiddle.net/bDePR/

If you search for "President" or "Secretary", you'll see the behavior I'm describing.

Answer №1

Here is a straightforward approach I devised: leverage the jQuery :visible selector to locate all visible tr elements (after sorting), then effortlessly add alternating CSS styles!

// reset the background
$j('tr').css('background', '#ccc');
$j('tr:visible').each(function(i){
    if((i % 2) == 0) {
        // apply background to every other one
        $j(this).css('background', 'yellow');
    }
});

Example: http://jsfiddle.net/bDePR/1/

Update:

This alternative method by @amustill achieves the same outcome in a more efficient and concise manner

// reset the background
$j('tr').css('background', '#ccc');
$j('.found').filter(':odd').css({ background: 'yellow' });

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

How can I make the <body> element take up the full screen space

Currently, I have implemented a radial gradient as the background image on my webpage using the following code: background-image: -webkit-gradient(radial, 100% 100%, 10, 90% 90%, 600, from(#ccc), to(#000)); Although this approach works fine, there is an ...

Steps for positioning a logo to the left and navigation to the right with HTML and CSS

Could someone assist me in aligning a logo on the left and navigation menu on the right using HTML and CSS? I envision it to look like the image displayed. I have tried various approaches to adjust my CSS styling, but nothing seems to be working as inten ...

After filling out the form, the user's entered information is instantly shown on the same page

I want to create a form where my friend can input information that will be displayed on the same page. For example, when he enters the title in a text field and clicks submit, it should appear in the title box. Additionally, he should be able to enter va ...

Is it possible to use JavaScript to forcefully transition a CSS keyframe animation to its end state?

One dilemma I am facing involves CSS keyframe animations triggered by scroll behavior. When users scroll too quickly, I would like JavaScript to help send some of the animations to their 'finished/final' state, especially since the animations bui ...

nsIStyleSheetService - The Gateway to Stylish CSS

I am currently utilizing nsIStyleSheetService within my Firefox extension to apply a custom stylesheet to every webpage. While everything is functioning properly, there are a few styles that are not being applied as expected. One of the styles causing iss ...

The use of JQuery's .show() and .hide() functions may disrupt the functionality of CSS hover

When working with this jsFiddle, it appears that the jQuery .show() function does not behave as expected. Despite the documentation stating that it should revert CSS attributes to their original values, using .show(x).delay(x).hide(x) seems to cause the CS ...

Designing exquisite button navigation

Hey everyone, I'm currently working on creating a circular button navigation for my website. I want to have 4 buttons, each with a different color, and I want them to glow when the user hovers over them. So far, this is what I have in my HTML: HTML: ...

Prevent clicking until the IE 10 and 9 windows have fully loaded

My goal is to prevent clicking on an image element until all the resources have finished loading, enabling the click only after the window.load() or when document.readystate reaches complete status. While this code works well in Chrome and Safari, I am fac ...

Adaptable young person occupying an adaptable receptacle

I'm having an issue with the automatic shrinking of children within a flexible container box. I want the elements with classes .c1 and .c2 to shrink when the page size is reduced, so that their container with class .a2 also shrinks and stays positione ...

Delete the line height (or leading) for bigger text in CSS

Is there a way to eliminate the space above and below the mandatory span, specifically when using << within it? The height of the field row is determined by the default line-height for the text size, but the mandatory field is taller due to its larg ...

What is the method to alter the background color of a whole row in a data table once the checkbox has been checked using CSS in JSP?

Here is the JSP page code snippet: <p:column selectionMode="multiple" exportable="false" style="text-align:center;"> </p:column> <p:ajax event="rowSelectCheckbox" update="deleteButton"/> <p:ajax event="rowUnselectCheckbox" update= ...

Change the width of both text boxes

As a newcomer to designing HTML forms, I am looking to create a simple input-group where the width of the text boxes is different. Specifically, I want the surname textbox to be shorter and the FullName textbox to be longer. Can anyone provide guidance on ...

The height of the <ul> element does not adjust to accommodate its child <li> elements

The <ul> on my page has a height:100%, but it doesn't dynamically adjust to fit all of its <li> child elements. This is what's happening: When scrolling through the list, you'll notice that the background color for the <ul& ...

Issue with Bootstrap 5: Mobile Menu Failure to Expand

I’ve been struggling with this issue for days now. I’ve searched everywhere for a solution, but to no avail. That’s why I’m turning to the experts here for help :-) The problem I’m facing is that my mobile menu won’t open. Nothing happens when ...

Printing long contents on Chrome's ion-modal-view is not supported on every page

I have tried various solutions found in articles but have not been successful. Here is an example of one such attempt: @media print { .modal { position: absolute; left: 0; top: 0; margin: 0; padding: 0; visibility: visible; / ...

Is the z-index problem in Safari related to a transform issue?

I've tried different solutions for similar issues, but I'm struggling to get the text to appear properly on my custom buttons in Safari. They are displaying perfectly in Chrome and Firefox, but not on iOS devices. Does anyone have any suggestion ...

Tips for ensuring that your sidebar remains contained within the boundaries of the outer div

Here is my Vue code utilizing bootstrap-vue. I am attempting to place the sidebar within the card body, but it's not displaying as intended. How can I adjust it to fit properly inside either the outer div or the b-card body? <template> <di ...

HTML list with padding

I need to display two lists side by side. In one list, I want to insert some space between two li elements (an empty li). For example, I am aiming for something that looks like this: A B A B A A B I tried adding an empty li, which worked ...

CSS - Creating a stylish break line for link boxes

Struggling with creating a box that includes linked text and a line break. However, the issue arises when the line breaks, causing the box to also break. After trying multiple options, I still can't seem to find a solution. Check out my attempt her ...

Carousel malfunctioning, refusing to slide smoothly

I recently copied the bootstrap 5 Carousel code and tweaked it with some additions, however, it's not functioning correctly. It seems to only go to the 2nd slide and then stops. Strangely enough, when I open the developer tools (F12) and go to inspect ...