Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1082483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:18:17+00:00 2026-05-16T22:18:17+00:00

I am trying to apply css rules to tr elements depending on what class

  • 0

I am trying to apply css rules to tr elements depending on what class would be the following tr.

ex.

<table>
<tr class="x"/>
<tr class="y"/>
<tr class="x"/>
<tr class="x"/>
</table>

So those tr with class x that have a following tr with class y will have different css style than those tr that the next tr is with class x.

<script type="text/javascript">
 $(document).ready(function () {
  if ($("tr.x").next('(tr.y)')) {

        $(this).css({'background-color' : 'yellow', 'font-weight' : 'bolder'});

      }
  else{

       $(this).css({'background-color' : 'blue', 'font-weight' : 'normal'});

      }
});
</script>

It is not working at all and I don’t know why.

Any help would be appreciated.

Thanks in advance.

Update

I wanted to apply the css style to the first td of the tr, and I am trying;

$(this).first('td').css({ 'color': 'Blue',});

But still apply it to the row.

Thanks again to all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:18:18+00:00Added an answer on May 16, 2026 at 10:18 pm

    My updated answer is inspired by Sarfraz’s original answer, but he went a different way in the end, so posting here instead:

    $('tr.x:has(+ tr.y)').css({ ... });
    

    That uses a sibling combinator with a :has test. (I’m not sure whether :has is a Sizzle-specific thing [Sizzle being the selector engine used by jQuery and others] or of there’s a proposal out there; it’s not specified by CSS3, and so not standard…yet?)

    Live example: http://jsbin.com/imeso4/3

    Update If you want to apply styling to the first child cell of that row, you’re looking for the :first-child selector:

    $('tr.x:has(+ tr.y) td:first-child').css({ ... });
    

    Live example: http://jsbin.com/imeso4/8

    Or if you want to do something with the rows, and then also do something with the cells, break it up:

    var rows = $('tr.x:has(+ tr.y)');
    rows.css({color: 'blue'});
    rows.find('td:first-child').css({color: 'red'});
    

    Live example: http://jsbin.com/imeso4/7


    Original (plodding) answer:

    $(document).ready(function() {
    
        $('tr.x').each(function() {
            var $this = $(this);
            var next = $this.next('tr');
            if (next.length > 0 && next.hasClass('y')) {
                // This tr is class x followed by class y
            }
        });
    
    });
    

    Live example: http://jsbin.com/imeso4

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to apply CSS styles to page elements that are dynamically inserted in
i have many fields in my form i was trying to apply different css
I am trying to apply some CSS class on the options of a drop
I'm trying to apply unknown styles to unknown selectors and it would seem that
I'm trying to apply a CSS mask to an element that also has child
I'm trying to find a way to apply a css class to the first
My table has id=mytable . I'm trying to apply some CSS on the first
I am trying to apply css to facebook like button. I know that it
I know that IE doesn't apply css style to table rows, so I had
I am trying to apply css for my table as show in sample 9.Table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.