Question

I have a table like:

<table id="table">
    <tbody>
        <tr></tr>
        <tr>
            <td>
                <table>
                    <tbody>
                        <tr></tr>
                    </tbod>
                </table>
            </td>
        </tr>
     </tbody>
</table>

I'm using jQuery and there exists a stored selector to get the most outer table:

var x = $('#table')

Starting from that If want to get all first level <tr>-elements.

If I use one of those:

x.find('tbody > tr');
x.children('tbody').children();

… the first one will naturally select all nested <tr>-elements as well. The latter seems over-complicated and involves multiple queries.

Is there a way to make this faster/more efficient?

Was it helpful?

Solution

First thing, x.find('tbody > tr') would find all <tr>s. You would need to do x.find('> tbody > tr'), assuming x is x from your example.

I ran a test and this with both and this was my finding.

.children(): 3.013ms
>: 0.626ms

so the > method is faster than the .children() method. The function calls add up... barely.

Here's my JavaScript for the testing.

var $table = $('#table'), $usingChildren, $usingAngleBracket;

console.time('.children()');
$usingChildren = $table.children('tbody').children('tr');
console.timeEnd('.children()');

console.time('>');
$usingAngleBracket = $table.find('> tbody > tr');
console.timeEnd('>');

console.log( $usingChildren, $usingAngleBracket );

OTHER TIPS

the fastest way to get direct children of a parent is .children, so what you can do is:

$('tbody').children('tr')

.find() will search child of child too, so you may not want to use that.

Use can use jQuery's .first() method to find the first <tr> element,

$('#mytable tr').first()

Although, as you wish to find the first <tr> that has nested child elements, you can filter it with .has(). For example: http://jsfiddle.net/cwL4q/3/

$("#mytable tr").has('tbody').first().css("background-color", "red" ); 

Although, I would strongly suggest simply labelling the 'nested' <tr>'s with a class, then you can simply access them much quicker as you know.

$('.nestedrow');

For the HTML below:

<table id="table">
        <tbody>
            <tr></tr>
            <tr class="nestedrow">
                <td>
                    <table>
                        <tbody>
                            <tr></tr>
                        </tbod>
                    </table>
                </td>
            </tr>
         </tbody>
    </table>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top