jquery - Highlight duplicate row values JavaScript

Jquery - Highlight duplicate row values JavaScript

To highlight duplicate row values in a table using jQuery, you can follow these steps:

  1. Iterate through each row in the table.
  2. Check for duplicate values in the specified column(s).
  3. Apply a CSS class to highlight the cells that contain duplicate values.

Here's an example of how to achieve this:

HTML Structure

Assuming you have a table like this:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Highlight Duplicate Row Values</title> <style> .highlight { background-color: yellow; } </style> </head> <body> <table id="myTable" border="1"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Email</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>John</td> <td>john@example.com</td> </tr> <tr> <td>2</td> <td>Jane</td> <td>jane@example.com</td> </tr> <tr> <td>3</td> <td>John</td> <td>john.doe@example.com</td> </tr> <tr> <td>4</td> <td>Jane</td> <td>jane@example.com</td> </tr> </tbody> </table> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="script.js"></script> </body> </html> 

JavaScript (script.js)

$(document).ready(function() { // Function to highlight duplicates in the specified column index function highlightDuplicates(columnIndex) { var values = {}; $('#myTable tbody tr').each(function() { var cell = $(this).find('td').eq(columnIndex); var value = cell.text(); if (values[value]) { values[value].push(cell); } else { values[value] = [cell]; } }); $.each(values, function(key, cells) { if (cells.length > 1) { $.each(cells, function(index, cell) { cell.addClass('highlight'); }); } }); } // Call the function to highlight duplicates for the 'Name' column (index 1) highlightDuplicates(1); }); 

Explanation

  1. HTML Structure: A simple table with thead and tbody. Each row contains three columns: ID, Name, and Email.

  2. CSS for Highlighting: A CSS class .highlight is defined to set the background color to yellow for highlighting duplicate cells.

  3. JavaScript/jQuery:

    • Function to Highlight Duplicates: The highlightDuplicates function takes the column index as a parameter and highlights the duplicate values in that column.
      • Collect Cell Values: Iterate through each row and collect the values of the specified column in an object. If a value appears more than once, store all the cells containing that value.
      • Highlight Duplicates: Iterate through the collected values and add the highlight class to cells containing duplicate values.
    • Initialize: Call the highlightDuplicates function for the 'Name' column (index 1).

Usage

  • Column Index: Adjust the column index in the highlightDuplicates function call to check for duplicates in different columns (e.g., 2 for Email).

This approach highlights the cells with duplicate values in the specified column, making it easy to identify duplicates visually.

Examples

  1. jQuery highlight duplicate row values in a table

    • Description: Implement jQuery to identify and highlight duplicate row values in a table based on specific criteria.
    // Example: Highlight duplicate cells in a table based on text content $('table tr').each(function() { var seen = {}; $(this).find('td').each(function() { var txt = $(this).text(); if (seen[txt]) { $(this).addClass('duplicate'); } else { seen[txt] = true; } }); }); 
  2. jQuery highlight duplicate rows by class

    • Description: Use jQuery to apply a class to duplicate rows in a table based on their values.
    // Example: Highlight duplicate rows in a table based on cell content var seen = {}; $('table tr').each(function() { var row = $(this); var key = row.find('td').text(); if (seen[key]) { row.addClass('highlight'); } else { seen[key] = true; } }); 
  3. jQuery find and highlight duplicate values

    • Description: Implement jQuery to find and highlight duplicate values within specific elements on a webpage.
    // Example: Highlight duplicate values in a list or container var seen = {}; $('.container div').each(function() { var text = $(this).text(); if (seen[text]) { $(this).addClass('duplicate'); } else { seen[text] = true; } }); 
  4. jQuery highlight duplicate values in a list

    • Description: Use jQuery to identify and visually mark duplicate values within a list element.
    // Example: Highlight duplicate values in an unordered list (ul) var seen = {}; $('ul li').each(function() { var text = $(this).text(); if (seen[text]) { $(this).addClass('duplicate'); } else { seen[text] = true; } }); 
  5. jQuery highlight duplicate cells in a table

    • Description: Apply jQuery to highlight duplicate cells within each row of an HTML table.
    // Example: Highlight duplicate cells in each row of a table $('table tr').each(function() { var seen = {}; $(this).find('td').each(function() { var txt = $(this).text(); if (seen[txt]) { $(this).addClass('duplicate'); } else { seen[txt] = true; } }); }); 
  6. jQuery highlight duplicate values in a div

    • Description: Use jQuery to identify and visually mark duplicate values within a div container.
    // Example: Highlight duplicate values within a div var seen = {}; $('.container').find('span').each(function() { var text = $(this).text(); if (seen[text]) { $(this).addClass('duplicate'); } else { seen[text] = true; } }); 
  7. jQuery highlight duplicate text elements

    • Description: Implement jQuery to highlight duplicate text elements based on their content.
    // Example: Highlight duplicate text elements var seen = {}; $('p').each(function() { var text = $(this).text(); if (seen[text]) { $(this).addClass('duplicate'); } else { seen[text] = true; } }); 
  8. jQuery highlight duplicate rows in a dynamic table

    • Description: Use jQuery to dynamically identify and highlight duplicate rows in an HTML table.
    // Example: Highlight duplicate rows in a dynamically generated table $(document).on('click', '#checkDuplicates', function() { var seen = {}; $('table tr').each(function() { var key = $(this).text(); if (seen[key]) { $(this).addClass('duplicate'); } else { seen[key] = true; } }); }); 
  9. jQuery highlight duplicate row values on button click

    • Description: Implement jQuery to highlight duplicate row values in a table upon clicking a button.
    // Example: Highlight duplicate row values on button click $('#highlightDuplicates').click(function() { var seen = {}; $('table tr').each(function() { var key = $(this).text(); if (seen[key]) { $(this).addClass('highlight'); } else { seen[key] = true; } }); }); 
  10. jQuery identify and highlight duplicate rows

    • Description: Use jQuery to identify and visually mark duplicate rows in an HTML table based on their content.
    // Example: Identify and highlight duplicate rows in a table var seen = {}; $('table tr').each(function() { var row = $(this); var key = row.find('td:first').text(); if (seen[key]) { row.addClass('highlight'); } else { seen[key] = true; } }); 

More Tags

calayer passport.js spring-mybatis xhtmlrenderer operators celery delphi-xe8 soapui lex information-visualization

More Programming Questions

More Weather Calculators

More Gardening and crops Calculators

More Retirement Calculators

More Entertainment Anecdotes Calculators