tags:

views:

426

answers:

3

I have a aspx page that looks something like this:

<table runat="server" id="tblTEst">
  <tr id="trHeader" runat="server">

  </tr>
  <tr id="trRow1" runat="server">

  </tr>
  <tr id="trRow2" runat="server">

  </tr>
</table>

When I write my JQuery to say get a count of s, I need to do:

alert($('#' + strTableId).children('tbody').children('tr').length);

My question is when does the TBODY get added? When I do a InnerHTML, I do see the TBODY. I was wondering if this is something ASP.NET does?

EDIT Thanks for the answers. If it's added by the browser, do I need to worry about testing the code in multiple browsers to ensure compatibility of JQuery? I was under the impression, JQuery is compatible with all browsers and I would'nt have to worry about testing the code on different browsers.

+2  A: 

It's neither ASP.Net nor jQuery, it's your browser. jQuery accesses the DOM of your page which is built by your browser based of the HTML code generated by ASP.Net. For tables, when it is not explicity declared, a TBODY element is added to hold "body" rows (TR) of a table.

ybo
+1  A: 

It's something the browsers do, as part of the process of cleaning up the HTML while building the DOM. The standard specifies that TR elements must occur within a TBODY, THEAD, or TFOOT element, and since you specified none of those, the browser helps you out by adding one for you.

If you wish to ignore this, you can re-write your expression as:

alert($('#' + strTableId).find('tr').length);

...But, you would do well to explicitly wrap your rows in TBODY so as to avoid confusion in the future.

Shog9
+1  A: 

The best thing would be to properly construct your table with a tbody on the server side. Then, you wouldn't have to worry about inconsistencies between browsers.

Dave Ward