Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

jquery clone table row

/*
<table>
  <tr>
    <td>one</td>
    <td><button type="button" class="js-clone-row">duplicate</button></td>
  </tr>
  <tr>
    <td>two</td>
    <td><button type="button" class="js-clone-row">duplicate</button></td>
  </tr>
</table>
*/

$(function() {
  /** Note: Assumes we are called from somewhere inside a <tr> */
  function cloneClosestTableRow(e) {
    const closestTableRow = $(e.currentTarget).closest('tr');
    const clone = closestTableRow.clone(true);
    const targetContainer = closestTableRow.parent();
    targetContainer.append(clone)
  }
  $('.js-clone-row').on('click', cloneClosestTableRow)
});
 
PREVIOUS NEXT
Tagged: #jquery #clone #table #row
ADD COMMENT
Topic
Name
4+7 =