jQuery: skipping first table row

javascript, jquery

Solution

In terms of performance, using `.find()` will be better than resolving the selector with Sizzle.

$('#tblTestAttributes').find('tbody').find('tr').each(function () { ... });

Here's the jsPerf to show it.

Problem

This is the HTML: ``` <table id="tblTestAttributes"> <thead> <tr> <th>Head 1</th> <th>Head 2</th> </tr> </thead> <tbody> <tr> <td id="txtDesc">Item 1</td> <td id="ddlFreq">Assume a DropDownList Here</td> </tr> <tr> <td id="txtDesc">Item 1</td> <td id="ddlFreq">Assume a DropDownList Here</td> </tr> <tr> <td id="txtDesc">Item 1</td> <td id="ddlFreq">Assume a DropDownList Here</td> </tr> </tbody> </table> ``` This is the javascript to get the values of each row: ``` var frequencies = []; if ($('#tblTestAttributes').length) { $('#tblTestAttributes tr').each(function () { var t = $(this).find('td[id^="txtDesc"]').text() + ";" + $(this).find('[id^="ddlFreq"] option:selected').val(); alert(t); frequencies.push(t); }); } ``` I want to avoid the first row, which contains `th` elements which are just display headers and don't contain any data. So I changed the selector to this: ``` #tblTestAttributes tr:not(:first-child) ``` This is skipping the second `tr` as well. What is happening here?

Original source