an easy way to get all table rows from a table without using a loop

advertisements

Is there an easy way to get all the table rows from a table without using a loop.

I thought that this would work but it only alerts the first row.

http://jsfiddle.net/THPWy/

    $(document).ready(function () {

        var O = $('#mainTable').find('tr');
        //var O = $('#mainTable tr');

        alert(O.html());

       //alerts     <th>Month</th><th>Savings</th> 

    });

<table id ="mainTable" border="1">
  <caption>Monthly savings</caption>
  <tr>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
  <tr>
    <td>February</td>
    <td>$50</td>
  </tr>
  <tr>
    <td>March</td>
    <td>$50</td>
  </tr>
  <tr>
    <td>a</td>
    <td>$50</td>
  </tr>
  <tr>
    <td>m</td>
    <td>$50</td>
  </tr>
  <tr>
    <td>j</td>
    <td>$50</td>
  </tr>
  <tr>
    <td>july</td>
    <td>$50</td>
  </tr>
  <tr>
    <td>aug</td>
    <td>$50</td>
  </tr>
  <tr>
    <td>sep</td>
    <td>$50</td>
  </tr>
</table>


What about:

// get all tr (excluding the caption)
var O = $('table#mainTable').children().slice(1);

http://jsfiddle.net/THPWy/7/