JustBeingHelpful
JustBeingHelpful

Reputation: 18980

jQuery 1.7.1: length function not working to get table tr count

This code stopped working when we upgraded to jQuery 1.7.1. $('table.className > tr').length is returning 0. Can you tell me how to correct it?

.NET code (executes jQuery code):

public static long GetTableRowCountByCssClass(IWebDriver driver, string cssClass, int exclusionRowCount)
{
    IJavaScriptExecutor js = driver as IJavaScriptExecutor;
    long count = (long)js.ExecuteScript("return $('table." + cssClass + " > tr').length");
    if (count != 0)
    {
        count = count - exclusionRowCount;
    }
    return count;
}

HTML code:

<!-- Products -->
<table class="cart" cellpadding="0" cellspacing="0" border="0">

    <thead>
        <tr>
            <th class="remove">Delete</th>
            <th class="prod-desc">Product Description</th>
            <th class="ships_w">Ships Within</th>
            <th class="price">Unit Price</th>
            <th>&nbsp;</th>
            <th class="quantity">Qty.</th>
            <th class="totals">Total</th>
        </tr>
    </thead>

    <tfoot>
        <tr>
            <td id="subtotal" class="order_summary_label" colspan="6" >Sub Total: </td>
            <td id="subtotal_number" class="order_summary_value">$124.89</td>
        </tr>
        <tr>
            <td id="cart-total-label" class="order_summary_label" colspan="6" >Total: </td>
            <td id="cart-total" class="order_summary_value"><strong>$124.89</strong></td>
        </tr>
    </tfoot>

    <tbody>

        <tr class="alt">

            <td class="remove">
                <input class="checkIt" type="checkbox" name="remove[variation][208]" id="remove_208_variation" value="0"  />
            </td>

            <td class="prod-desc">
            <div class="imgThmb">
            <a href="/product/water-gel" title="Water Gel - Magic Slush Powder - Water Gel 100 gram Jar">
            <img src="/img/cache/product/WSPA_500_140_64.jpg" alt="Water Gel - Magic Slush Powder - Water Gel 100 gram Jar" />
            </a>
            </div>

                <a href="/staging/product/water-gel" title="Water Gel - Magic Slush Powder - Water Gel 100 gram Jar" class="clean">
                    Water Gel - Magic Slush Powder - Water Gel 100 gram Jar                                                     
                </a>
            </td>

            <td class="ships_w">
            </td>

            <td class="price">$6.99</td>

            <td class="discount">
            </td>

            <td class="quantity">
                <input type="text" class="quantity_box quantity" name="quantities[variation][208]" id="quantity_variation_208" value="5" maxlength="4" />
            </td>

            <td class="totals"><strong>$34.95</strong></td>

        </tr>

        <tr >

            <td class="remove">
                <input class="checkIt" type="checkbox" name="remove[variation][1118]" id="remove_1118_variation" value="0"  />
            </td>

            <td class="prod-desc">
            <div class="imgThmb">
            <a href="/product/flying-film-canisters" title="Flying Film Canisters Kit - Flying Film Canisters Activity Kit">
            <img src="/img/cache/product/film_canister_launcher_2011011240_64.jpg" alt="Flying Film Canisters Kit - Flying Film Canisters Activity Kit" />
            </a>
            </div>

            <a href="/product/flying-film-canisters" title="Flying Film Canisters Kit - Flying Film Canisters Activity Kit" class="clean">
            Flying Film Canisters Kit - Flying Film Canisters Activity Kit                                                      
            </a>
            </td>

            <td class="ships_w">
            </td>

            <td class="price">$14.99</td>

            <td class="discount">
            </td>

            <td class="quantity">
                <input type="text" class="quantity_box quantity" name="quantities[variation][1118]" id="quantity_variation_1118" value="6" maxlength="4" />
            </td>

            <td class="totals"><strong>$89.94</strong></td>

        </tr>

    </tbody>

</table>

Upvotes: 1

Views: 1115

Answers (1)

David Thomas
David Thomas

Reputation: 253416

The problem is that the tr is not, and cannot be, a direct child of the table element. It's a child of the tbody element that's automatically placed in the table by the browser if it's not put there by the author of the page.

If you try, instead: $('table.className > tbody > tr').length it should work.

Upvotes: 8

Related Questions