I am working on a form with dynamically added inputs, by using the cloned method. Each input will need a datepicker.
The name of each input will be distinct for output.
I am using the Jquery UI Datepicker for this. The problem that occurs (the message in the console) is "$cloned.find(...).attr(...) is undefined"
I went to another post jQuery DatePicker not working on newly added row
and tried removing the hasDatepicker class (included in code below), but the problem persists.
Thanks for any input or resources.
The code is below:
<table>
<tr>
<td name="a" class="FormLabel" width="100px"><strong>Class Year*</strong></td>
<td width="100px"><input type="text" id="datepicker" name="ClassYear_1" class="usedatepicker" value="" tabindex="4" /></td>
</tr>
</table>
<button type="button" id="add-btn" name="add-btn">Add Class Year</button>
JS Code:
function clone2(){
var $cloned = $('table tr:last').clone();
$cloned.removeClass('hasDatepicker').datepicker();
//console.log($(this));
//alert($(this).attr('name'));
var oldIndex = $cloned.find('input').attr('name').match(/\d+/);
var newIndex = parseInt(oldIndex,10)+1;
$cloned.find('input').each(function(){
var newName = $(this).attr('name').replace(oldIndex, newIndex);
$(this).attr('name', newName);
});
$cloned.insertAfter('table tr:last');
}
$('#add-btn').click( function() {
clone2();
});
//attach datepicker - begin
$(document).ready(function() {
$(".usedatepicker").datepicker(); // or
$(".usedatepicker").datepicker("refresh");
});