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

  • SEARCH
  • Home
  • 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 7629201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:46:30+00:00 2026-05-31T05:46:30+00:00

I have a page which contains several tables, and I would like to format

  • 0

I have a page which contains several tables, and I would like to format each table so that the second cell of each other row has a specific background color.
I have tried the following jQuery code as suggested in an answer to the initial version of this question :

   $('table tr:odd td:nth-child(2)').css("background-color", "#F6F3EE");

This works fine as long as all tables have an even number of rows. If that is not the case, the formatting is inverted, as if jQuery considered the s as being part of one single table and doesn’t reinitiate the counter at each table.

Here’s a link to a js fiddle to illustrate the problem:

http://jsfiddle.net/davidThomas/eAHUF/

  • 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-31T05:46:31+00:00Added an answer on May 31, 2026 at 5:46 am

    You’re selecting only the first td element that is the descendent of a tr.reg:even row.

    Try using the :nth-child() alternative:

    $('table tr:odd td:nth-child(2)').css('background-color','#ffa');​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
    

    JS Fiddle demo.

    Note: you have to remember that the CSS :nth-child() is one-based, not zero-based (as JavaScript is).


    Edited in response to OP’s comment, below:

    …but it presents me with a problem. I’ve edited your jsfiddle demo to add a row in the first table thus giving it an uneven number of rows. Now the second table’s rows are reversely highlighted which is not what I was looking for. Do you have a fix for this ?

    Yeah, I…missed the obvious (somehow). I’ve amended the above code to look at each table in turn and then look for the :odd rows and the nth-child():

    $('table').find('tr:odd td:nth-child(2)').css('background-color','#ffa');​
    

    JS Fiddle demo.

    References:

    • find().
    • :nth-child().
    • :odd selector.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have a page that contains several different forms all of which are
I have a ASPX Page that contains several user controls, which are loaded dynamically
I have a page which contains a html.RenderPartial, that renders an ASP.NET MVC partial
I have a fairly complex page which contains an absolutely positioned table (as the
I have a page which contains several forms. I want to add a single
I have a page which contains several vimeo videos, embedded in the standard iframe
In my web app, I have an aspx page which contains an html table
I have created one application which contains several buttons to home page clicking on
I have a web page which has three <div> elements: Right_bar contains several layouts
I have a page that contains a parent record and several children records. There

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.