I want to clone a table (Not a table Row) with textboxes and radio buttons in it.
More specifically, I want to clone whole table without textbox values but with radio button values.
Here, textbox and radio button values get removed while cloning. How can I obtain the desired behavior ?
Here, is my code :
<div>
<table id="tablePreviousHistory">
<tbody>
<tr>
<td>
<input id="pCountry" name="pCountry" value="" type="text">
</td>
<td>
<input id="pvisatype" name="pvisatype" value="" type="text">
</td>
<td>
<input id="pstartdate" name="pstartdate" value="" type="text">
</td>
<td>
<input id="pStatus" name="pStatus" value="Yes" type="radio">
Yes<br>
<input id="pStatus" name="pStatus" value="No" type="radio">
No
</td>
</tr>
</tbody>
</table>
</div>
Javascript:
<script>
$("#tablePreviousHistory").clone(true).attr('id', 'tablePreviousHistory' + k).find("input").val('').each(function () {
if (this.type=='radio') {
this.checked = false;
}
$(this).attr({
'id': function (_, id) { return id + k },
'name': function (_, name) { return name + k }
});
}).end().insertAfter("#tablePreviousHistory" + (k != 1 ? (k - 1).toString() : ''));
</script>
The text inputs are cleared in this line of code with
val('')
If you replace that line with this one below, then they should no longer clear.