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 8967685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:18:01+00:00 2026-06-15T17:18:01+00:00

I have a dynamic table that needs to assign each TD it’s own class.

  • 0

I have a dynamic table that needs to assign each TD it’s own class. In the example below, .one is being applied to all TDs for this table. Using just CSS (or Jquery), can I assign td classes .two and .three to the second and third children of this table?

[please do not add class names to TD; this is a dynamic table]

Dynamic – To clarify, the numbers of TDs/TRs in the table are unknown. Therefore, the CSS has to be smart enough to adjust regardless if there are 3 TDs/TRs or 10 TDs/TRs.

HTML:

<table id="foo">
  <tr>
   <td>One</td>
  </tr>
  <tr>
   <td>Two</td>
  </tr>
  <tr>
   <td>Three</td>
  </tr>
</table>​

CSS:

 #foo{
   position: absolute;
   display: block;
   background: gray;
   color: white;
   height: 67px;
   width: 500px;
   border: 1px solid black;
}

tr {
   width: 500px;
   border: 1px solid black;
}

.one td {
   background: red;
   display: block;
   position: relative;
   left: 0px;
   width: 15px;
   border: 1px solid black;
   height: 19px;
   text-indent: 22px;
}

.two td {
   background: green;
   display: block;
   position: relative;
   left: 0px;
   width: 15px;
   border: 1px solid black;
   height: 19px;
   text-indent: 22px;
}

See JS Fiddle here: http://jsfiddle.net/bigthyme/kM7H9/

  • 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-06-15T17:18:02+00:00Added an answer on June 15, 2026 at 5:18 pm

    You can use each elements index among the set as a lookup:

    ​var classes = ['one', 'two', 'three'];
    
    $("#foo td").attr("class", function (index, value) {
        return classes[index]; // 0 returns 'one', 1 returns 'two', etc.
    });​​​​​​​​​​​​
    

    Demo: http://jsfiddle.net/SzKbh/1/

    You don’t need to do this though; you can target them without using a class:

    tr:nth-of-type(1) td { color: red }
    tr:nth-of-type(2) td { color: green }
    tr:nth-of-type(3) td { color: blue }​
    

    Demo: http://jsfiddle.net/SzKbh/

    Which has pretty good support in modern browsers. This does require you to explicitly set the colors for each row, and thus know how many rows you’ll have. If you don’t know how many rows you’ll have you can use a more complicated selector:

    table tr:nth-child(2n+0) { color: red }
    table tr:nth-child(2n+1) { color: blue }
    table tr:nth-child(3n+0) { color: green }
    

    Demo: http://jsfiddle.net/SzKbh/2/

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

Sidebar

Related Questions

I have two table cells, with dynamic content. Table cell one works fine, when
Fiddle: http://jsfiddle.net/MhWm5/16/ I have some dynamically generated table rows/values with dynamic IDs <td class=control-group>
Using storyboard, I have a table view controller containing multiple dynamic prototype cells. One
I have table that I want to remain dynamic meaning that I don't want
I have a dynamic table on my page that a user can add rows/delete
I'm using SQL Server 2k8 and have a table that needs to be able
I am trying to create a dynamic table that is being built based on
I have a Dynamic table that I want to give color to on alternative
I have some PHP code that generates dynamic tables of data on the fly.
What im doing is I have a dynamic table and creating rows which all

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.