views:

86

answers:

5

I do like this on my document ready:

$("thead").addClass("ui-state-default");
$("tbody").addClass("ui-state-highlight");

And I noticed that the class is added also to the tables that don't have tbody, I tested on all browsers. Should I ever bother to write tbody when i generate/write my HTML?

+8  A: 

Yes, I would include <thead> and <tbody> elements to ensure consistency, just in case you run into some browser out there that does not automatically generate them for you.

patrick dw
+2  A: 

I tend personally to include it when I think about it or explicitly need it, but you don't fail XHTML 1 Strict for not including it.

DavidYell
+6  A: 

The tbody element is required in HTML, the start and end tags for it are, however, optional (as they are for a number of other elements, including html, head and body).

In XHTML the element is optional, presumably to be compatible with markup people were writing so they wouldn't have to learn to stop letting the element be implied.

In text/html mode, most browsers will (correctly) add the element to the DOM automatically. This can have implications for anything which makes assumptions about what elements in tables are children or parents of other elements (e.g. JS or CSS).

Including the element in the markup explicitly could reduce confusion and maintenance issues, so it is worthwhile but far from essential.

If you are working with XHTML, it becomes more important since the element may or may not appear depending on if the document is processed as text/html or application/xhtml+xml. I would always include it explicitly in an XHTML document.

David Dorward
+1  A: 

Yes you should. it's good form. and I've had trouble with some plugins not working quite right when i didn't explicitly write the tbody and thead tags in. (the jquery tablesorter). It could have been something unrelated. but is the extra 20 characters so much work to type?

Patricia
A: 

The practical advantage of tbody shows when you're building tables with grouped rows. Then, you can easily identify groups (e.g. via a jQuery selector) by addressing the right tbody.

Dan Stocker