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 3315380
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:20:36+00:00 2026-05-17T22:20:36+00:00

Before I dive in to the jQuery/Sizzle source I thought i’d ask here about

  • 0

Before I dive in to the jQuery/Sizzle source I thought i’d ask here about ways to speed the below method up.

This is a standard “Check All” checkbox scenario. A header cell (<thead><tr><th>) has a checkbox that when checked checks all other checkboxes in it’s table’s tbody that are in the same column.

This works:

// We want to check/uncheck all columns in a table when the "select all"
// header checkbox is changed (even if the table/rows/checkboxes were 
// added after page load).
(function () {
    // use this dummy div so we can reattach our table later.
    var dummy = $("<div style=display:none />");

    // hook it all up!
    body.delegate(".js_checkAll", "change", function () {

        // cache selectors as much as possible...
        var self = $(this),
            // use closest() instead of parent() because 
            // the checkbox may be in a containing element(s)
            cell = self.closest("th"),
            // Use "cell" here to make the "closest()" call 1 iteration 
            // shorter. I wonder if "parent().parent()" would be faster 
            // (and still safe for use if "thead" is omitted)?
            table = cell.closest("table"),
            isChecked,
            index;

        // detaching speeds up the checkbox loop below.
        // we have to insert the "dummy" div so we know
        // where to put the table back after the loop.
        table.before(dummy).detach();

        index = cell.index();
        isChecked = this.checked;

        // I'm sure this chain is slow as molasses
        table
            // get only _this_ table's tbody
            .children("tbody")
            // get _this_ table's trs
            .children()
            // get _this_ table's checkboxes in the specified column
            .children(":eq(" + index + ") :checkbox")
            // finally...
            .each(function () {
                this.checked = isChecked;
            });

        // put the table back and detach the dummy for
        // later use
        dummy.before(table).detach();

    });
} ());

However, for 250+ rows, it starts to become slow (at least on my machine). Users may need to have up to 500 rows of data so paging the data isn’t the solution (items are already paged @ 500/page).

Any ideas how to speed it up some more?

  • 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-17T22:20:37+00:00Added an answer on May 17, 2026 at 10:20 pm
    // I wonder if "parent().parent()" would be faster 
    // (and still safe for use if "thead" is omitted)?
    

    No. If <thead> is omitted then in HTML a <tbody> element will be automatically added, because in HTML4 both the start-tag and the end-tag are ‘optional’. So in HTML, it would be parent().parent().parent(), but in XHTML-served-as-XML, which doesn’t have the nonsense that is optional tags, it would be parent().parent().

    It’s probably best to stick with closest(). It’s clearer, it’s not particularly slow and you’re only using it once, so it’s not critical anyway.

    index = cell.index();
    

    Although, again, this is only once per table so not critical, there is a standard DOM property to get the index of a table cell directly, which will be faster than asking jQuery to search and count previous siblings: index= cell[0].cellIndex.

    // we have to insert the "dummy" div so we know
    // where to put the table back after the loop.
    

    That’s a bit ugly. Standard DOM has a better answer to this: remember the element’s parentNode and nextSibling (which may be null if this is the last sibling) and when you’re done you can parent.insertBefore(table, sibling).

            .children("tbody")
            .children()
            .children(":eq(" + index + ") :checkbox")
            .each(function () {
                this.checked = isChecked;
            });
    

    You should consider using .children().eq(index) rather than hiding that away in a selector. Won’t make a big difference, but it’s a bit clearer.

    In any case, you can save jQuery’s selector engine a bunch of work by using some more standard DOM to traverse the table:

    $.each(table[0].tBodies[0].rows, function() {
        $(this.cells[index]).find('input[type=checkbox]').each(function() {
            this.checked = isChecked;
        });
    });
    

    Selector queries can be fast, when they’re operating against the document and using only standard CSS selectors. In this case jQuery can pass the work onto the browser’s fast document.querySelectorAll method. But scoped selectors (find and the second argument to $()) can’t be optimised due to a disagreement between jQuery and Selectors-API over what they mean, and non-standard Sizzle selectors like :eq and :checkbox will just get rejected. So this:

    $('#tableid>tbody>tr>td:nth-child('+(index+1)+') input[type=checkbox]')
    

    could actually be faster, on modern browsers with querySelectorAll!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before I dive really deep into MongoDB for days, I thought I'd ask a
Before I dive into the disscusion part a quick question; Is there a method
I'm new to iOS and objective-C. Before I dive down into this I'd like
Before I dive into development, I want to know if this is possible at
Before I dive into ASIHttpRequest's source code: From the documentation ( http://allseeing-i.com/ASIHTTPRequest/How-to-use ) of
Now before I dive too far into this this question, I am aware of
This is just a quick question before I dive deeper into converting my current
Before I make the dive and make it myself, is there any open source
Just wanted to know before I dive into this field. Is it possible to
I'm new to WPF and before I dive in solving a problem in completely

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.