I'm using JS to highlight duplicate values in a table.
What basically the code is doing is adding table rows value in an array and then comparing if it exist and then highlights that row.
The code is working fine but it highlights all duplicate values in the same color(red). What i need it to do is to highlight each group of similar values in different color. Lets say i have 4 group of duplicate values, each group should be highlighted in a different color. Maybe colors need to be generated randomly as there maybe multiple duplicate values in the table.
$(function() {
var tableRows = $("#sortable tbody tr"); //find all the rows
var rowValues = []; //to keep track of which values appear more than once
tableRows.each(function() {
var rowValue = $(this).find(".content").html();
if (!rowValues[rowValue]) {
var rowComposite = new Object();
rowComposite.count = 1;
rowComposite.row = this;
rowValues[rowValue] = rowComposite;
} else {
var rowComposite = rowValues[rowValue];
if (rowComposite.count == 1) {
$(rowComposite.row).css('backgroundColor', 'red');
}
$(this).css('backgroundColor', 'red');
rowComposite.count++;
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="sortable">
<tbody>
<tr>
<td class="content">A</td>
</tr>
<tr>
<td class="content">B</td>
</tr>
<tr>
<td class="content">A</td>
</tr>
<tr>
<td class="content">C</td>
</tr>
<tr>
<td class="content">C</td>
</tr>
</tbody>
</table>