{"id":5927,"date":"2024-12-16T08:44:55","date_gmt":"2024-12-16T08:44:55","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=5927"},"modified":"2025-09-03T05:50:43","modified_gmt":"2025-09-03T05:50:43","slug":"how-to-add-table-row-in-jquery","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/","title":{"rendered":"How to Add Table Row in jQuery"},"content":{"rendered":"\n<p>HTML tables are widely used in almost every website and application these days. Often, web developers need to be able to add one or more rows to HTML tables on their web pages. There are several ways to do this using plain JavaScript as well as using third-party libraries such as jQuery. In this article, we will learn how to add table row in jQuery.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#The_Problem\" >The Problem<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#How_to_Add_Table_Row_in_jQuery\" >How to Add Table Row in jQuery<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#1_Using_append_with_table_tag\" >1. Using append() with table tag<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#2_Using_append_with_tbody\" >2. Using append() with tbody<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#3_Using_after\" >3. Using after()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#4_Using_prepend\" >4. Using prepend()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#5_Using_before\" >5. Using before()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#6_Using_Clone\" >6. Using Clone()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#7_Using_JavaScript\" >7. Using JavaScript<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Problem\"><\/span>The Problem<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Typically, web developers need to be able to dynamically insert table rows when the page is loaded, or when an event such as button click is fired. jQuery offers several powerful functions such as append(), prepend(), before() and after() to add table rows. But their result depends on the existing table structure as well as the position where you want to insert the new row. If you do not use the appropriate jQuery function, you may end up with undesirable result. So you need to be careful about what jQuery function you use to add new row to HTML tables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Add_Table_Row_in_jQuery\"><\/span>How to Add Table Row in jQuery<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are several ways to insert table row in jQuery. But there are several important nuances to keep in mind while working with tables. We will cover them below.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Using_append_with_table_tag\"><\/span>1. Using append() with table tag<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Append() function allows you to append HTML content at the end of specific page elements. Depending on the jQuery selector, it will append the code at the end of one or more elements. We can use this to add a table row in jQuery. Let us say you have the following HTML table, where you only have row tags tr inside the table tag<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;table id=\"my_table\"&gt;<br>    &lt;tr&gt;&lt;td&gt;1&lt;\/td&gt;&lt;\/tr&gt;<br>    &lt;tr&gt;&lt;td&gt;2&lt;\/td&gt;&lt;\/tr&gt;<br>&lt;\/table&gt;<\/pre>\n\n\n\n<p>In the above case, you can easily use append() function to add another row to this table, as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$(''#my_table').append('&lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;');<\/pre>\n\n\n\n<p>In the above code, we select the table tag using its CSS selector and call append() function. In it, we pass the HTML code for the new row. You will end up with the following result.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;table id=\"my_table\"&gt;<br>    &lt;tr&gt;&lt;td&gt;1&lt;\/td&gt;&lt;\/tr&gt;<br>    &lt;tr&gt;&lt;td&gt;2&lt;\/td&gt;&lt;\/tr&gt;<br>    &lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;<br>&lt;\/table&gt;<\/pre>\n\n\n\n<p>This works only if your HTML table has only table tag and no tbody tag.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Using_append_with_tbody\"><\/span>2. Using append() with tbody<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>On the other hand, if your table also has a tbody element, as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;table id=\"my_table\"&gt;<br>   &lt;tbody&gt;<br>    &lt;tr&gt;&lt;td&gt;1&lt;\/td&gt;&lt;\/tr&gt;<br>    &lt;tr&gt;&lt;td&gt;2&lt;\/td&gt;&lt;\/tr&gt;<br>   &lt;\/tbody&gt;<br>&lt;\/table&gt;<\/pre>\n\n\n\n<p>If you call append() function on table, as shown above, then you will get the following table. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;table id=\"my_table\"&gt;<br>   &lt;tbody&gt;<br>    &lt;tr&gt;&lt;td&gt;1&lt;\/td&gt;&lt;\/tr&gt;<br>    &lt;tr&gt;&lt;td&gt;2&lt;\/td&gt;&lt;\/tr&gt;<br>   &lt;\/tbody&gt;<br><strong>   &lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;<\/strong><br>&lt;\/table&gt;<\/pre>\n\n\n\n<p>In this case, the new row will be appended after tbody element, which is not desirable. Therefore, you need to modify your append() function to be called on tbody element, instead of table element.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$('#my_table &gt; tbody').append('&lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;');<\/pre>\n\n\n\n<p>Now your table will look like the following.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;table id=\"my_table\"&gt;<br>   &lt;tbody&gt;<br>    &lt;tr&gt;&lt;td&gt;1&lt;\/td&gt;&lt;\/tr&gt;<br>    &lt;tr&gt;&lt;td&gt;2&lt;\/td&gt;&lt;\/tr&gt;<br> <strong>   &lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;<\/strong><br>   &lt;\/tbody&gt;<br>&lt;\/table&gt;<\/pre>\n\n\n\n<p>This will also work if your table has a tfoot tag after tbody tag, with one or more footer rows in it.<\/p>\n\n\n\n<p>If you have an HTML table with its tbody tag is somewhere inside, then you can use find() function to find the tbody tag and call append() function on it. Here is an example to illustrate it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$(\"#my_table\").find('tbody').append('&lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;');<\/pre>\n\n\n\n<p><strong>Nested tables<\/strong><\/p>\n\n\n\n<p>Sometimes, you may even have nested HTML tables, one inside the other, with each one having a separate tbody tag.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;table id='my_table'&gt;<br>  &lt;tbody id='outer'&gt;<br>    &lt;tr&gt;&lt;td&gt;<br>      &lt;table &gt;<br>        &lt;tbody id='inner'&gt;<br>          ...<br>        &lt;\/tbody&gt;<br>      &lt;\/table&gt;<br>    &lt;\/td&gt;&lt;\/tr&gt;<br>  &lt;\/tbody&gt;<br>&lt;\/table&gt;<\/pre>\n\n\n\n<p>In such cases, you will need to determine whether you want to add new row to outer table or inner table.<\/p>\n\n\n\n<p>If you need to add new row to the outside table, then call append() function on &#8216;tbody:first&#8217; that is outer table.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$('#my_table &gt; tbody:first').append('&lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;'); <\/code><\/pre>\n\n\n\n<p>The above code will append new row to the first, that is, outer tbody tag.<\/p>\n\n\n\n<p>If you want to add new row to the inner table, then call append() function on &#8216;tbody:last&#8217; that is the inner tbody tag.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$('#my_table &gt; tbody:last').append('&lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;'); <\/pre>\n\n\n\n<p>The above code will append new row to the last, that is, inner tbody tag.<\/p>\n\n\n\n<p>Please note, your table will have tbody only if it has at least 1 row. If it has no rows, then it may only have table tag and no tbody tag. So depending on the situation, you will need to call append() function on table or tbody tag.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Using_after\"><\/span>3. Using after()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can also use after() function for this purpose. It allows you to add HTML content immediately after a specific element.<\/p>\n\n\n\n<p>Let us say your table has multiple tbody tags.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;table id='my_table'&gt;<br>  &lt;tbody&gt;<br>    &lt;tr&gt;...&lt;\/tr&gt;<br>  &lt;\/tbody&gt;<br>  &lt;tbody&gt;<br>    &lt;tr&gt;...&lt;\/tr&gt;<br>  &lt;\/tbody&gt;<br>&lt;\/table&gt;<\/pre>\n\n\n\n<p>If your table has multiple tbody tags, then calling append() function on tbody tag will end up adding new row to each tbody tag separately. This is not what we need. In this case, you can look for the last tr tag, that is the last row, and call after() function on it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$('#my_table &gt; <strong>tr:last<\/strong>').after('&lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;');<\/pre>\n\n\n\n<p>Here is the table you will get.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;table id='my_table'&gt;<br>  &lt;tbody&gt;<br>    &lt;tr&gt;...&lt;\/tr&gt;<br>  &lt;\/tbody&gt;<br>  &lt;tbody&gt;<br>    &lt;tr&gt;...&lt;\/tr&gt;<br><strong>    &lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;<\/strong><br>  &lt;\/tbody&gt;<br>&lt;\/table&gt;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Using_prepend\"><\/span>4. Using prepend()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In jQuery, append() function allows you to add a new row at the end of a specific element, such as table. On the other hand, prepend() function allows you to add a new row at the beginning of specific element. Let us say you have the following HTML table.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;table id=\"my_table\"&gt;<br>    &lt;tr&gt;&lt;td&gt;1&lt;\/td&gt;&lt;\/tr&gt;<br>    &lt;tr&gt;&lt;td&gt;2&lt;\/td&gt;&lt;\/tr&gt;<br>&lt;\/table&gt;<\/pre>\n\n\n\n<p>You can easily add a new row at the beginning of table, as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$(''#my_table').prepend('&lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;');<\/pre>\n\n\n\n<p>In this case, you will get the following result.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;table id=\"my_table\"&gt;<br><strong>    &lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;<\/strong><br>    &lt;tr&gt;&lt;td&gt;1&lt;\/td&gt;&lt;\/tr&gt;<br>    &lt;tr&gt;&lt;td&gt;2&lt;\/td&gt;&lt;\/tr&gt;<br>&lt;\/table&gt;<\/pre>\n\n\n\n<p>If your table also has tbody element inside it, then you will need to call the prepend() function on the tbody instead of table. Let us say you have the following HTML table with tbody tag in it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;table id=\"my_table\"&gt;<br>   &lt;tbody&gt;<br>    &lt;tr&gt;&lt;td&gt;1&lt;\/td&gt;&lt;\/tr&gt;<br>    &lt;tr&gt;&lt;td&gt;2&lt;\/td&gt;&lt;\/tr&gt;<br>   &lt;\/tbody&gt;<br>&lt;\/table&gt;<\/pre>\n\n\n\n<p>In this case, you need to call prepend() function as shown, on tbody tag.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$('#my_table &gt; tbody').prepend('&lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;');<\/pre>\n\n\n\n<p>Then you will get the following table.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;table id=\"my_table\"&gt;<br>   &lt;tbody&gt;<br><strong>    &lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;<\/strong><br>    &lt;tr&gt;&lt;td&gt;1&lt;\/td&gt;&lt;\/tr&gt;<br>    &lt;tr&gt;&lt;td&gt;2&lt;\/td&gt;&lt;\/tr&gt;<br>   &lt;\/tbody&gt;<br>&lt;\/table&gt;<\/pre>\n\n\n\n<p>Let&#8217;s say you have nested tables, one inside the other, with each table having a tbody tag.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;table id='my_table'&gt;<br>  &lt;tbody id='outer'&gt;<br>    &lt;tr&gt;&lt;td&gt;<br>      &lt;table &gt;<br>        &lt;tbody id='inner'&gt;<br>          ...<br>        &lt;\/tbody&gt;<br>      &lt;\/table&gt;<br>    &lt;\/td&gt;&lt;\/tr&gt;<br>  &lt;\/tbody&gt;<br>&lt;\/table&gt;<\/pre>\n\n\n\n<p>If you want to add new row to the outer table, then you need to call prepend() function tbody:first.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$('#my_table &gt; tbody:first').prepend('&lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;'); <\/pre>\n\n\n\n<p>In this case, you will get the following result.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;table id='my_table'&gt;<br>  &lt;tbody id='outer'&gt;<br>    &lt;tr&gt;&lt;td&gt;<br>      &lt;table &gt;<br>        &lt;tbody id='inner'&gt;<br>          ...<br>        &lt;\/tbody&gt;<br>      &lt;\/table&gt;<br>    &lt;\/td&gt;&lt;\/tr&gt;<br>  <strong>&lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;<\/strong><br>  &lt;\/tbody&gt;<br>&lt;\/table&gt;<\/pre>\n\n\n\n<p>On the other hand, if you want to add a new row to inner most table&#8217;s tbody tag, then you need to call prepend() function on tbody:last selector.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$('#my_table &gt; tbody:last').prepend('&lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;'); <\/pre>\n\n\n\n<p>In this case, you will get the following result.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;table id='my_table'&gt;<br>  &lt;tbody id='outer'&gt;<br>    &lt;tr&gt;&lt;td&gt;<br>      &lt;table &gt;<br>        &lt;tbody id='inner'&gt;<br>          ...<br>          <strong>&lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;<\/strong><br>        &lt;\/tbody&gt;<br>      &lt;\/table&gt;<br>    &lt;\/td&gt;&lt;\/tr&gt;<br>  &lt;\/tbody&gt;<br>&lt;\/table&gt;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5_Using_before\"><\/span>5. Using before()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Just as after() function allows you to add a new row after a specific element, before() function allows you to add a new row before a specific element. Let us say you have the following table with multiple tbody elements. Let us say you have the following HTML table.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;table id='my_table'&gt;<br>  &lt;tbody&gt;<br>    &lt;tr&gt;...&lt;\/tr&gt;<br>  &lt;\/tbody&gt;<br>  &lt;tbody&gt;<br>    &lt;tr&gt;...&lt;\/tr&gt;<br>  &lt;\/tbody&gt;<br>&lt;\/table&gt;<\/pre>\n\n\n\n<p>Let us say you want to add a new row before the first row of first tbody tag. You can do this using the following code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$('#my_table &gt; <strong>tr:first<\/strong>').before('&lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;');<\/pre>\n\n\n\n<p>Here is the HTML table you will get.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;table id='my_table'&gt;<br>  &lt;tbody&gt;<br><strong>    &lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;<\/strong><br>    &lt;tr&gt;...&lt;\/tr&gt;<br>  &lt;\/tbody&gt;<br>  &lt;tbody&gt;<br>    &lt;tr&gt;...&lt;\/tr&gt;<br>  &lt;\/tbody&gt;<br>&lt;\/table&gt;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6_Using_Clone\"><\/span>6. Using Clone()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>jQuery also provides clone() function that allows you to clone any HTML element. We can use this to clone a table row and append it back to the table. Let us say you have the following table, with rows also having ID attribute.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;table id='my_table'&gt;<br>   ...<br>   &lt;tr id='my_row'&gt;...&lt;\/tr&gt;<br>   ...<br>&lt;\/table&gt;<\/pre>\n\n\n\n<p>You can clone the above row and add it back to the table with the following code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$(\"#my_row\").clone().removeAttr(\"id\").appendTo( $(\"#my_row\").parent() );<\/pre>\n\n\n\n<p>In the above code, we call clone() function on row with id=my_row. We remove the ID attribute of the cloned row, to avoid duplicate ID values. We call appendTo() function on this cloned row to append it to the table. The $(&#8220;#my_row&#8221;).parent() means the parent element of said row, that is, the HTML table element.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"7_Using_JavaScript\"><\/span>7. Using JavaScript<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Sometimes, you may not have jQuery included on your web pages. In this case, you can add table row using plain JavaScript. Here is the code to do this.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">table_object.insertRow(index)<\/pre>\n\n\n\n<p>In the above code, index is the number that is used to specify the position to insert new row. It starts with 0, 1, 2&#8230;. If you use -1 then it will automatically insert the row at the end. Here is the JavaScript to select our table by its ID attribute. It will return table object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">document.getElementById(\"my_table\")<\/pre>\n\n\n\n<p>We call insertRow() function on this object. We set its innerHTML property to the new row&#8217;s HTML.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">document.getElementById(\"my_table\").insertRow(-1).innerHTML = '&lt;tr&gt;&lt;td&gt;3&lt;\/td&gt;&lt;\/tr&gt;';<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt many simple ways to easily add table row in jQuery. You can use any of them as per your requirement. We have also covered many special cases to help you work with real-world scenarios. Also, we have learnt how to use different jQuery functions, depending on the position of new row. Unfortunately, there is no one line solution to this problem. Depending on your situation, you will need to use the appropriate solution.<\/p>\n\n\n\n<p>Also read:<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-disable-enable-input-in-jquery\/\">How to Disable\/Enable Input in jQuery<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-change-elements-class-with-javascript\/\">How to Change Element&#8217;s Class with JavaScript<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Often web developers need to add one or more rows to HTML tables on their web pages. Here is how to add table row in jQuery.<\/p>\n","protected":false},"author":1,"featured_media":5964,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[350],"tags":[353],"class_list":["post-5927","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery","tag-add-table-row"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Add Table Row in jQuery - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Often web developers need to add one or more rows to HTML tables on their web pages. Here is how to add table row in jQuery.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Add Table Row in jQuery - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Often web developers need to add one or more rows to HTML tables on their web pages. Here is how to add table row in jQuery.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2024-12-16T08:44:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-09-03T05:50:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/add-table-row.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"274\" \/>\n\t<meta property=\"og:image:height\" content=\"183\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How to Add Table Row in jQuery\",\"datePublished\":\"2024-12-16T08:44:55+00:00\",\"dateModified\":\"2025-09-03T05:50:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/\"},\"wordCount\":1366,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/add-table-row.jpg?fit=274%2C183&ssl=1\",\"keywords\":[\"add table row\"],\"articleSection\":[\"jQuery\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/\",\"url\":\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/\",\"name\":\"How to Add Table Row in jQuery - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/add-table-row.jpg?fit=274%2C183&ssl=1\",\"datePublished\":\"2024-12-16T08:44:55+00:00\",\"dateModified\":\"2025-09-03T05:50:43+00:00\",\"author\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Often web developers need to add one or more rows to HTML tables on their web pages. Here is how to add table row in jQuery.\",\"breadcrumb\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/add-table-row.jpg?fit=274%2C183&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/add-table-row.jpg?fit=274%2C183&ssl=1\",\"width\":274,\"height\":183,\"caption\":\"add table row in jquery\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ubiq.co\/tech-blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Add Table Row in jQuery\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#website\",\"url\":\"https:\/\/ubiq.co\/tech-blog\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/\"],\"url\":\"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Add Table Row in jQuery - Ubiq BI","description":"Often web developers need to add one or more rows to HTML tables on their web pages. Here is how to add table row in jQuery.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/","og_locale":"en_US","og_type":"article","og_title":"How to Add Table Row in jQuery - Ubiq BI","og_description":"Often web developers need to add one or more rows to HTML tables on their web pages. Here is how to add table row in jQuery.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2024-12-16T08:44:55+00:00","article_modified_time":"2025-09-03T05:50:43+00:00","og_image":[{"width":274,"height":183,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/add-table-row.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How to Add Table Row in jQuery","datePublished":"2024-12-16T08:44:55+00:00","dateModified":"2025-09-03T05:50:43+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/"},"wordCount":1366,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/add-table-row.jpg?fit=274%2C183&ssl=1","keywords":["add table row"],"articleSection":["jQuery"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/","url":"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/","name":"How to Add Table Row in jQuery - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/add-table-row.jpg?fit=274%2C183&ssl=1","datePublished":"2024-12-16T08:44:55+00:00","dateModified":"2025-09-03T05:50:43+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Often web developers need to add one or more rows to HTML tables on their web pages. Here is how to add table row in jQuery.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/add-table-row.jpg?fit=274%2C183&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/add-table-row.jpg?fit=274%2C183&ssl=1","width":274,"height":183,"caption":"add table row in jquery"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-add-table-row-in-jquery\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How to Add Table Row in jQuery"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/add-table-row.jpg?fit=274%2C183&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-1xB","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/5927","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=5927"}],"version-history":[{"count":45,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/5927\/revisions"}],"predecessor-version":[{"id":9451,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/5927\/revisions\/9451"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/5964"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=5927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=5927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=5927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}