clarkk
clarkk

Reputation: 27685

Get all rows in the "current" table, and not from child tables

How can you get all rows in a table without getting the rows in child tables?

var rows = $('tr', tbl);

This will return ALL <tr> tags, including all rows in child tables.

Upvotes: 18

Views: 71731

Answers (4)

Foram Shah
Foram Shah

Reputation: 115

If you just want the count of rows, you can simply use:

var table = document.getElementById('tableID');  
table.rows.length;  

Or you can use direct children selector:

$('table > tbody > tr').each(function(index, tr) {} );

Upvotes: 1

Luty
Luty

Reputation: 170

var count = $('#tableID').rows;

It works, because the selector will return a HTMLTableElement object.

Upvotes: 4

FishBasketGordo
FishBasketGordo

Reputation: 23142

var rows = $('#tblID > tbody > tr')

The child selector will get the table's <tbody> element and consequently get the <tr> elements that are direct children of the table's tbody.

If you already have a table object:

var rows = $(tbl).find('> tbody > tr');

Or:

var rows = $(tbl).children('tbody').children('tr');

Here is a working example.

Upvotes: 49

Jorge Guberte
Jorge Guberte

Reputation: 11054

Probably:

var rows = $("#tableid>tr");

Upvotes: 2

Related Questions