Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>EDIT:</strong> There are some issues with accessing properties of cloned elements. I changed the answer to use the native <code>getAttribute</code> and <code>setAttribute</code> instead.</p> <pre><code>jQuery('#addRow').click(function() { // Get current count of imageTables and use that value for IDs var quantity = jQuery("table[id^=imageTable]").length; // clone the table var clone = jQuery("#imageTable").clone(true); // use native DOM methods to update the ID clone[0].setAttribute('id', clone[0].getAttribute('id') + quantity); // find any text or file inputs, and iterate over them clone.find(':text,:file').each(function() { // use native DOM methods to update the ID this.setAttribute('id', this.getAttribute('id') + quantity); // set the value to "" this.value = ""; }); // append to the &lt;td&gt; clone.appendTo('#mainTable &gt; tbody:last &gt; td'); });​ </code></pre> <hr> <p><strong>Original answer:</strong></p> <p>Try this:</p> <pre><code>&lt;script type="text/javascript"&gt; jQuery(document).ready(function() { jQuery('#addRow').click( function () { // Get current count of imageTables and use that value for IDs var quantity = jQuery("table[id^=imageTable]").length; // Clone the main table jQuery("#imageTable").clone(true) // Change its ID to the current ID plus the quantity variable .attr( 'id', function() { return this.id + quantity; }) // find any text or file inputs .find( ':text,:file' ) // change their IDs .attr( 'id', function() { return this.id + quantity; }) // set the input values to "" .val( "" ) // return to the cloned table .end() // append wherever you want it. // As the comment below your question states, // this is not a valid placement .appendTo('#mainTable &gt; tbody:last'); }) }); &lt;/script&gt; </code></pre> <p><strong>EDIT:</strong> Fixed typo in <code>.find()</code> where the comma was outside the quotation marks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload