diff --git a/src/manipulation.js b/src/manipulation.js
index 9b4f5e451..4f79f8998 100644
--- a/src/manipulation.js
+++ b/src/manipulation.js
@@ -47,11 +47,12 @@ var
rscriptTypeMasked = /^true\/(.*)/,
rcleanScript = /^\s*\s*$/g;
+// Prefer a tbody over its parent table for containing new rows
function manipulationTarget( elem, content ) {
if ( jQuery.nodeName( elem, "table" ) &&
jQuery.nodeName( content.nodeType !== 11 ? content : content.firstChild, "tr" ) ) {
- return elem.getElementsByTagName( "tbody" )[ 0 ] || elem;
+ return jQuery( ">tbody", elem )[ 0 ] || elem;
}
return elem;
diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js
index 911852451..b1c04dbd5 100644
--- a/test/unit/manipulation.js
+++ b/test/unit/manipulation.js
@@ -2730,6 +2730,26 @@ QUnit.test( "Make sure col element is appended correctly", function( assert ) {
assert.strictEqual( table.find( "td" ).width(), 150 );
} );
+QUnit.test( "Make sure tr is not appended to the wrong tbody (gh-3439)", function( assert ) {
+ assert.expect( 1 );
+
+ var htmlOut,
+ htmlIn =
+ " ",
+ newRow = "" +
+ "
" +
+ "nested