Highlighte table cells

Kabdar 1 Reputation point
2023-03-15T09:00:29.1633333+00:00

When the 'add line' button is clicked, I want to check the values of each cell in all rows in my table and if the value is 0 or empty, i want to highlight those cells.

I tried something like this, but it doesn't work:

var $row = jQuery('#tblOrderLines').closest('tr');
        var $columns = $row.find('td');

        $columns.addClass('row-highlight');
        var values = "";

        jQuery.each($columns, function (i, item) {
            values = values + 'td' + (i + 1) + ':' + item.innerHTML + '<br/>';
            alert(values);
        });
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,238 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
862 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 25,231 Reputation points Microsoft Vendor
    2023-03-16T08:41:41.39+00:00

    Hi @Kabdar ,

    You can try to use the following code:

    function addOrderLine() {
            var table = document.getElementById('tblOrderLines');
            for (var r = 1, n = table.rows.length; r < n; r++) {
                for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
                    var x = table.rows[r].cells[c].getElementsByTagName('input')[0].value;
               
                    if (x == "" || x == 0) {
                        table.rows[r].cells[c].getElementsByTagName('input')[0].style.background = 'green'
                    }
                   
                }
               
            }
           
        }
    

    Test output:

    User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.