Reputation: 1781
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
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
Upvotes: 1
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
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