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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:40:41+00:00 2026-06-05T20:40:41+00:00

This is usually a very straight forward thing to do, so before you down

  • 0

This is usually a very straight forward thing to do, so before you down vote me for such a question hear me out 🙂

My problem is not selecting the elements and adding the class, it’s that I want the alternate class to be added to even or odd rows relative to that table.

Right now I am using this:

jQuery(document).ready(function(){

jQuery('table.rep tbody > tr td:first-child').addClass('composer');
jQuery('table.rep tbody > tr td:last-child').addClass('pieces');
jQuery('table.rep tbody > tr:odd').addClass('alt');

});

The problem is that jQuery is selecting all of the rows from all of the tables with the “rep” class and then adding the “alt” class to the odd rows from that set.

Like I said, I want to have it apply the class to the even/odd rows relative to each table, so the first row would always be styled the same way. As it is using the code above, the first rows can be either alt/normal based on how many rows are in the previous table.

I was thinking of somehow using the .each() function, but after looking through the jQuery docs, I haven’t been able to find out how to achieve my desired result.

I’m thinking it might look something like this:

jQuery('table.rep').each(function(){
    /* select/filter odd rows from this set and add alt class */
});

So my question is if .each() can be used like that and if so, how to drill down into the results of each iteration and make it do what I want.

  • 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-05T20:40:43+00:00Added an answer on June 5, 2026 at 8:40 pm

    Here you go..! Always remember that $(…).length is key to find out how may elements are in your filter or trap… 🙂

    Javascript:

    jQuery(document).ready(function()
    {
        $('table.rep tbody > tr td:first-child').addClass('composer');
        $('table.rep tbody > tr td:last-child').addClass('pieces');
        $('table.rep').each(function()
        {
           //alert($(this).children('tbody').children(':odd').length)
           $(this).children('tbody').children(':odd').addClass('alt');
        });
    
    });
    

    HTML

    <table border="1" class="rep">
        <tbody>
            <tr>
                <td>composor #1.1</td>
                <td>data</td>
                <td>data</td>
                <td>pieces #1.1</td>
            </tr>
            <tr>
                <td>composor #1.2</td>
                <td>data</td>
                <td>data</td>
                <td>pieces #1.2</td>
            </tr>
            <tr>
                <td>composor #2.1</td>
                <td>data</td>
                <td>data</td>
                <td>pieces #2.1</td>
            </tr>
            <tr>
                <td>composor #2.2</td>
                <td>data</td>
                <td>data</td>
                <td>pieces #2.2</td>
            </tr>
        </tbody>
    </table>
    <br /><br />
    <table border="1" class="rep">
        <tbody>
            <tr>
                <td>composor #1.1</td>
                <td>data</td>
                <td>data</td>
                <td>pieces #1.1</td>
            </tr>
            <tr>
                <td>composor #1.2</td>
                <td>data</td>
                <td>data</td>
                <td>pieces #1.2</td>
            </tr>
            <tr>
                <td>composor #2.1</td>
                <td>data</td>
                <td>data</td>
                <td>pieces #2.1</td>
            </tr>
    
        </tbody>
    </table>
    <br /><br />
    <table border="1" class="rep">
        <tbody>
            <tr>
                <td>composor #1.1</td>
                <td>data</td>
                <td>data</td>
                <td>pieces #1.1</td>
            </tr>
            <tr>
                <td>composor #1.2</td>
                <td>data</td>
                <td>data</td>
                <td>pieces #1.2</td>
            </tr>
            <tr>
                <td>composor #2.1</td>
                <td>data</td>
                <td>data</td>
                <td>pieces #2.1</td>
            </tr>
            <tr>
                <td>composor #2.2</td>
                <td>data</td>
                <td>data</td>
                <td>pieces #2.2</td>
            </tr>
        </tbody>
    </table>
    

    CSS

    .composer {
        color:blue;
    }
    .pieces {
        color:green;
    }
    .alt {
        background-color:#ddd;
    }
    body {
        font-family:tahoma
    }
    .rep td {
        padding:2px;
    }
    

    I could have avoided adding HTML but than in order to make this post (answer) self-contained – I am adding it here.

    You can view demo here on jsfiddle: http://jsfiddle.net/dharmavir/HfwqH/

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

Sidebar

Related Questions

Setting focus on a form element using JavaScript is usually very straight forward. I
very simple question. Lol i am embarassed to ask this cause i am usually
I usually use this line to import file from out of the current folder
I've been doing research on compilers. The lexer seems to be very straight forward:
The problem is pretty straightforward, I'm just not very well-versed in these tools (or
I have been seeing code like this usually in the start of header files:
I am rx this error on Deserialization: Invalid wire-type; this usually means you have
I have a crawler that for HTTPs switches over to Wininet. This usually works
How would I usually represent this business logic in a graph? A is true
After creating a new form, I usually perform this ritual: Change the name into

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.