After clicking a button, I tried to clone form elements and append them to the target using this code.
$('#addChild').on('click', function () {
var num = $('.clonedInput').length,
newNum = new Number(num + 1),
newElem = $('#entry' + num).clone().attr('id', 'entry' + newNum).fadeIn('slow');
if (newNum == 4) {
alert("Sorry, You can add upto 3 childrens only");
return false;
}
newElem.find('.heading-reference').attr('id', 'ID' + newNum + '_reference').attr('name', 'ID' + newNum + '_reference').html('Child #' + newNum);
// Title - select
newElem.find('.fnameLabel').attr('for', 'ID' + newNum + '_title');
newElem.find('.fName').attr('id', 'ID' + newNum + '_fName').attr('name', 'ID' + newNum + '_fName').val('');
// First name - text
newElem.find('.surLabel').attr('for', 'ID' + newNum + '_sName');
newElem.find('.sName').attr('id', 'ID' + newNum + '_sName').attr('name', 'ID' + newNum + '_sName').val('');
// Last name - text
newElem.find('.genderLabel').attr('for', 'ID' + newNum + '_gender');
newElem.find('.genderSelect').attr('id', 'ID' + newNum + '_gender').attr('name', 'ID' + newNum + '_gender');
// Color - checkbox
newElem.find('.dobLab').attr('for', 'ID' + newNum + '_dobLab');
newElem.find('.dob').attr('id', 'ID' + newNum + '_dob').attr('name', 'ID' + newNum + '_dob');
$('#entry' + num).after(newElem);
$('#ID' + newNum + '_title').focus();
$('#btnDel').attr('disabled', false);
});
However, I encountered an issue where the selectric select box does not work for cloned items. I'm unsure of where I've gone wrong. Any assistance on this matter would be greatly appreciated.
If you'd like to see the code in action, please visit this link.