Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make function give row that have different value with color red to full row ?

Status
Not open for further replies.

ahmedsa2018

Programmer
Apr 25, 2018
67
EG
problem


How to make row that have different value with color red font to full row ?


I have html dynamic table not static meaning i dont know how many rows or column inside

table because it changed based on data show from back end .

I need to give color red to row have distinct value

according to my case then second row and third row will be

have font red and first row will not be change color .

First row have similar value as 12 on all td so that color will not change .

But second row have distinct value or different value 15 so that will be red font to full row completely .

third row have distinct value or different value as 17,15,13,12 so that third row

completely will have red font .

meaning if I have one value or more different value from each other on same row

then full row or all td on tr will be change font to red .


Actually i need function jquery or javascript give row that have different value with

color red to full row ?

can you please help me on that ?




HTML:
<!DOCTYPE html>
<html>
<body>
<table border="1">
<col width="500">
<col width="500">
<col width="500">
<col width="500">
<tr bgcolor="#6699FF" width="100%">
    <th>Part1</th>
    <th>Part2</th>
    <th>Part3</th>
    <th>Part4</th>
<tr>
    <td>12</td>
    <td>12</td>
    <td>12</td>
    <td>12</td>
</tr>
<tr>
    <td>12</td>
    <td>15</td>
    <td>12</td>
    <td>12</td>
</tr>
<tr>
    <td>17</td>
    <td>15</td>
    <td>13</td>
    <td>12</td>
</tr>
</table>

<button id="button">Click Me</button>
</body>
</html>

 
Hi

As I believe that You might not need jQuery, here is a plain JavaScript solution :
Code:
[b]function[/b] [COLOR=orange]color_row[/color][teal]([/teal]table[teal])[/teal]
[teal]{[/teal]
    [b]for[/b] [teal]([/teal][b]var[/b] row of table[teal].[/teal]rows[teal])[/teal]
        [b]if[/b] [teal]([/teal][b]new[/b] [COLOR=orange]Set[/color][teal]([/teal]value_list [teal]=[/teal] Array[teal].[/teal][COLOR=orange]from[/color][teal]([/teal]row[teal].[/teal]cells[teal]).[/teal][COLOR=orange]filter[/color][teal]([/teal]cell [teal]=>[/teal] cell[teal].[/teal]tagName [teal]==[/teal] [i][green]'TD'[/green][/i][teal]).[/teal][COLOR=orange]map[/color][teal]([/teal]cell [teal]=>[/teal] cell[teal].[/teal]textContent[teal])).[/teal]size [teal]>[/teal] [purple]1[/purple][teal])[/teal]
            row[teal].[/teal]style[teal].[/teal]backgroundColor [teal]=[/teal] [i][green]'red'[/green][/i]
[teal]}[/teal]

You call it by passing a reference to the table node to decorate, for example with your HTML I tested it like this :
JavaScript:
window[teal].[/teal][COLOR=orange]addEventListener[/color][teal]([/teal][i][green]'load'[/green][/i][teal], () =>[/teal] [COLOR=orange]color_row[/color][teal]([/teal]document[teal].[/teal][COLOR=orange]getElementsByTagName[/color][teal]([/teal][i][green]'table'[/green][/i][teal])[[/teal][purple]0[/purple][teal]]))[/teal]


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top