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>

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.
I’d suggest:
JS Fiddle demo.
Note the
tbodyin the selector, to prevent this applying to the first row, I moved that row into atheadelement, since it seems to be a header for the table.Note that, in the above, the
each()is, strictly speaking, unnecessary asaddClass()could be used with a function:JS Fiddle demo.
Edited to address the fact that I’d used
parseInt(), instead ofparseFloat()to handle the conversion of text to number:JS Fiddle demo.