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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:32:39+00:00 2026-06-10T01:32:39+00:00

I have a table with so many rows. it’s structure is like this code

  • 0

I have a table with so many rows. it’s structure is like this code

<table border="1" cellpadding="1" cellspacing="1" style="width: 500px">
   <tbody>
      <tr>
         <td>Name</td>
         <td>Criteria</td>
         <td>Grade</td>
      </tr>
      <tr>
         <td>Nick</td>
         <td>5</td>
         <td>5.75</td>
      </tr>
      <tr>
         <td>David</td>
         <td>3</td>
         <td>11.5</td>
      </tr>
   </tbody>
</table>

jquery table

I want if the grade was bigger than criteria, a CSS class named good be assigned to the <tr> otherwise the class name be bad (based on one <th> the <tr> will have a class name).
The runtime result will be like this (pay attention to line 11 and 19):

<table border="1" cellpadding="1" cellspacing="1" style="width: 500px">
   <tbody>
      <tr>
         <td>Name</td>
         <td>Criteria</td>
         <td>Grade</td>
      </tr>
      <tr class="good">
         <td>Nick</td>
         <td>5</td>
         <td>5.75</td>
      </tr>
      <tr class="bad">
         <td>David</td>
         <td>3</td>
         <td>2.5</td>
      </tr>
   </tbody>
</table>

Note that my intention is to highlight good bad result rows with CSS.

  • 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-10T01:32:40+00:00Added an answer on June 10, 2026 at 1:32 am

    I’d suggest:

    $('tbody tr').each(
        function(){
            var that = $(this),
                criteria = parseInt(that.find('td:eq(1)').text(),10),
                grade = parseInt(that.find('td:eq(2)').text(),10),
                trClass = grade > criteria ? 'good' : 'notGood';
            that.addClass(trClass);
        });​
    

    JS Fiddle demo.

    Note the tbody in the selector, to prevent this applying to the first row, I moved that row into a thead element, since it seems to be a header for the table.

    Note that, in the above, the each() is, strictly speaking, unnecessary as addClass() could be used with a function:

    $('tbody tr').addClass(function() {
        var that = $(this),
            criteria = parseInt(that.find('td:eq(1)').text(), 10),
            grade = parseInt(that.find('td:eq(2)').text(), 10);
            return grade > criteria ? 'good' : 'notGood';
    });​
    

    JS Fiddle demo.


    Edited to address the fact that I’d used parseInt(), instead of parseFloat() to handle the conversion of text to number:

    $('tbody tr').addClass(function() {
        var that = $(this),
            criteria = parseFloat(that.find('td:eq(1)').text()),
            grade = parseFloat(that.find('td:eq(2)').text());
            return grade > criteria ? 'good' : 'notGood';
    });
    

    JS Fiddle demo.

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

Sidebar

Related Questions

In each table have many number of rows with amount. Like this i have
Lets say I have a table containing many many rows like this: ID Range
I have a table in my page as below; <table id=tbl cellpadding=0 cellspacing=0 style=width:
I have a table with many rows on my page. I would like to
I have a MySQL table that contains many rows. The table structure is as
lets say i have very big table ( many rows) and i have this
I have a table with many rows and each row has select lists. Something
I have a table with many rows of data, and I want to show
I have a table with many rows, is there any way to find out
I have a data table with many rows and columns. How can I display

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.