Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This problem has a few interesting aspects:</p> <ol> <li><p>When a row is cloned, all items with an <code>id</code> attribute will be cloned as well, causing two elements with the same <code>#ID</code>; that's not good. It can be solved by creating a pristine sample row that gets cloned every time you click the button which you then have to "decorate" before it can be used (i.e. apply <code>.selectmenu()</code> and click handler).</p></li> <li><p>When a <code>&lt;select&gt;</code> is cloned, it doesn't retain the selected option. You have to save the <code>selectedIndex</code> property and apply it to the cloned version.</p></li> </ol> <p>Both problems solved looks like this:</p> <pre><code> $(document).ready(function() { // keep reference to the sample row var sample = $('.test_tr.sample'); // click handler for the clone button function cloneRow() { var parentRow = $(this).parents('.test_tr'), selectedOption = parentRow.find('.test_select').prop('selectedIndex'); setupRow(newRow().insertAfter(parentRow), selectedOption); } // decorates the new row and sets the correct selected option before applying // selectmenu() function setupRow(row, selectedOption) { row .find('.test_select') .prop('selectedIndex', selectedOption || 0) .selectmenu() .end() .find('.test_clone') .click(cloneRow) .end() } // helper function that clones the sample and shows it before handing it over // to other code. function newRow() { return sample.clone().show(); } // setup the first row setupRow(newRow().appendTo('table')); }); </code></pre> <p><a href="http://jsbin.com/itemip/2/" rel="nofollow"><strong>Demo</strong></a></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