clone form with values

by Craig Martin

HTML

<div class="custitem_cont"><div class="custitem"> <span class="custnumber"><i onclick="jQuery(this).closest('.custitem_cont').remove();" class="fa fa-trash-o trash_custitem_cont" aria-hidden="true"></i> (Custom 1) <button id="addthen_1_btn" onclick="AddThenAction('7038', 1)" class="more_actions_btn button-default">Add Additional "Then" Actions</button></span> If the answer to EID#: <span class="curEID">1</span> is <select style="width: auto !important;" class="custom_logic_if_answer_to_question_is"> <option value="" selected="" disabled="">Select one... </option> <option value="Yes">"Yes"</option> <option value="No">"No"</option> </select> ...and the answer to EID#: <input class="custom_logic_source_eid" type="text" name="custom_logic_source_eid" value="" placeholder="Enter EID Number"> is: <select name="eid_ans_is" class="eid_ans_is"> <option disabled="" selected="">Select one...</option> <option>Any answer type</option><option>Yes</option> <option>No</option> <option>an amount</option> <option>a percentage</option> </select> </div> <div class="custom_logic_source_ans_amt_or_perc_cont" style="display: none; float: left;"> ...and answer is <select class="CLSamt custom_logic_source_ans_amt_or_perc" name="custom_logic_source_ans_amt_or_perc"> <option disabled="" selected="">Select one...</option> <option>Any number</option> <option>greater than</option> <option>less than</option> <option>equal to</option> <option>not equal to</option> </select> <span class="amt_or_perc" style="display: none;"> this value: <input class="custom_logic_source_ans_amt_or_perc" type="text" name="custom_logic_source_ans_amt_or_perc_rez" value=""> </span> </div> <div class="custom_logic_then_action_cont"> <span style="padding-left: 4px;">...Then </span> <select class="custom_logic_then_action custitem_1_then" name="custom_logic_then_action"> <option disabled="" selected="">Select one...</option> <option>Stop here</option> <option>Skip to EID#</option> <option>Pre-fill other select...

JavaScript

jQuery('#butt').click(function(){
    
var $orginal = jQuery('.custitem_cont');
var $cloned = $orginal.clone();

//get original selects into a jq object
var $originalSelects = $orginal.find('select');
$cloned.find('select').each(function(index, item) {
     //set new select to value of old select
     jQuery(item).val( $originalSelects.eq(index).val() );

});
//get original textareas into a jq object
var $originalTextareas = $orginal.find('textarea');

$cloned.find('textarea').each(function(index, item) {
//set new textareas to value of old textareas
jQuery(item).val($originalTextareas.eq(index).val());
}); 
$cloned.appendTo('#clonedItem');
});