Timothy Clemans
Timothy Clemans

Reputation: 1781

Creating tables with jQuery without tbody

How do I create tables in jQuery without tbody being added to my code by jQuery? See http://jsfiddle.net/r7BFq/

$('table').html('<tr><td>A</td><td>B</td></tr>');
$('pre').text($('table').html());​

results in

<tbody><tr><td>A</td><td>B</td></tr></tbody>

I don't want that. I want:

<tr><td>A</td><td>B</td></tr>

Upvotes: 3

Views: 1335

Answers (3)

Tats_innit
Tats_innit

Reputation: 34107

Try this demo please http://jsfiddle.net/d8zVX/

Quote @undefined bruv: Actually browser creates the tbody element not jQuery

Further read this:

Why do browsers still inject <tbody> in HTML5?

Quote

For historical reasons, certain elements have extra restrictions beyond even the restrictions given by their content model.

A table element must not contain tr elements, even though these elements are technically allowed inside table elements according to the content models described in this specification. (If a tr element is put inside a table in the markup, it will in fact imply a tbody start tag before it.)

This will fit the need :)

Code

$('table').html('<tr><td>A</td><td>B</td></tr>');
var hulk = $('table').html().replace(/<\/?tbody>/g, '');;
$('pre').text(hulk);​

Working Image

enter image description here

Upvotes: 1

Jesse
Jesse

Reputation: 19

This works perfect:

HTML:

<div id="table_sample"></div>

JQUERY:

$('#table_sample').html('<tr><td>A</td><td>B</td></tr>');
$('#table_sample').text($('#table_sample').html());

Upvotes: 0

Rab
Rab

Reputation: 35582

Why Would you try to remove tbody. your browser is trying to add the part of valid html, you are missing. Is it not nice?

Upvotes: 2

Related Questions