Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7653759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:06:22+00:00 2026-05-31T12:06:22+00:00

I have a function that checks each cell in each row of a table

  • 0

I have a function that checks each cell in each row of a table for the edit-error class. If this is found in any cell of a particular row (The cell value hasn’t passed through validation) then I remove the edit-submit class from the entire row and unselect a checkbox which is in the first td of that row.

When there are a large number of rows (1,000+) I am struggling with performance so I thought I could try for loops but I am not an expert in JavaScript so am looking for help with this.

The original code is as follows:

var checkErrors = function () {
    $('#results tr:not(:first)').each(function () {
        $('td:first input:checkbox', $(this)).attr('checked', 'checked');
        $(this).addClass('edit-submit');

        $(this).find('td').each(function () {
            if ($(this).hasClass('edit-error')) {
                $('td:first input:checkbox', $(this).parent('tr')).removeAttr('checked');
                $(this).parent().removeClass('edit-submit');
            }
        });
    });
}

The table has an id of #results. What I do first is check all the checkboxes and add the edit-submit class to each row then loop through all the cells checking for errors.

I am looking for any help with performance improvements on this function please.

EDIT

It would have made sense to add the table html. Apologies for that here it is:

<table id="results">
    <tbody>
        <tr>
            <th>&nbsp;</th>
            <th>Policy Number</th>
            <th>Quota Code</th>
            <th>Contract Date</th>
        </tr>
        <tr>
            <td><input type="checkbox"></td>
            <td class="edit-error">ABC123%</td>
            <td>123</td>
            <td class="edit-error">99/99/9999</td>
        </tr>
        <tr class="edit-submit">
            <td><input type="checkbox" checked="checked"></td>
            <td>ABC123</td>
            <td>123</td>
            <td>16/03/2012</td>
        </tr>
    </tbody>
</table>

This would show on the page:

--------------------------------------------------
|   | Policy Number | Quota Code | Contract Date |
--------------------------------------------------
|   | ABC123%       | 123        | 99/99/9999    |
--------------------------------------------------
| X | ABC123        | 123        | 16/03/2012    |
--------------------------------------------------
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:06:23+00:00Added an answer on May 31, 2026 at 12:06 pm

    There are several ways to speed up your code without going to a plain JS for loop. To name just a few:

    • You already have a reference to the current row within the outer .each() loop so use that rather than calling $(this).parent() within the inner loop.
    • If you want to process just the td elements that have a particular class you can include the class in your selector rather than calling .hasClass().
    • Avoid calling the same selector more than once, by storing a reference to the returned jQuery object and/or by chaining.
    • When providing a context as the second parameter to $() you can pass a DOM element or this directly, no need to wrap it in another $() call.
    • If you know that there will always be a checkbox you can uncheck it directly to save a function call.

    So:

    var checkErrors = function () {
        $('#results tr:not(:first)').each(function () {
            var $tr = $(this),
                cb = $('td:first input:checkbox', this)[0];
    
            cb.checked = true;
    
            $tr.addClass('edit-submit')
               .find('td.edit-error').each(function () {
                    cb.checked = false;
                    $tr.removeClass('edit-submit');
               });
        });
    };
    

    However, the inner loop on the tds isn’t needed at all since you just need to test whether there are any tds in the row with the error class – you don’t need to actually loop through them all. Plus for good measure here’s one way to use a for loop rather than .each() – note that you can avoid the complicate not-first selector if you simply select all rows then loop from the second onwards:

    var checkErrors = function () {
        var $rows = $('#results tr'),
            i;
        for (i = 1; i < $rows.length; i++) {
            var $tr = $rows.eq(i),
                cb = $tr.find('td:first input:checkbox')[0];
    
            if ($tr.find('td.edit-error').length === 0) {
               cb.checked = true;
               $tr.addClass('edit-submit');
            } else {
               cb.checked = false;
               $tr.removeClass('edit-submit');
            };
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a javascript function that checks for a date range. Is there any
I have this code snippet inside a function that checks if an object exists
Basically I have code that checks if any checkboxes with a given class are
I have a function that checks if a cookie (by name) exists or not:
I have a javascript function (function1) that checks some global variables (can the user
I have a log function on my admin panel that checks user input for
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I have a script that checks the class (integer) of a , runs a
I'm trying to develop a JS function that creates a new row each time
I have a function in my login form that checks if the email and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.