I am working on jquery code to add dynamic fields to the list of my fields, i have couple of web links but most of them using using live command does not work as expected, all i am doing this inside the jquery UI Tabs
I gave a try with this code, as it works well but has couple of issues:
function trimNums(stringToTrim)
{
return stringToTrim.replace(/\d+$/,"");
}
function dupForm(divId, divClass, btnAdd, btnRm)
{
//alert(divId+' '+divClass);
var num = $(divClass).length;
var newNum = new Number(num + 1);
var i;
var newElem = $('#' + divId + num).clone().attr('id', divId + newNum);
for (i=0; i < newElem.children().length; i++)
{
var attrId = trimNums(newElem.children(':eq('+i+')').attr('id'));
var attrName = trimNums(newElem.children(':eq('+i+')').attr('name'));
newElem.children(':eq('+i+')').attr('id', attrId + newNum).attr('name', attrName + newNum);
}
$('#' + divId + num).after(newElem);
$('#' + btnRm).attr('disabled',false);
//if (newNum == 15)
//$('#' + btnAdd).attr('disabled','disabled');
}
function rmForm(divId, divClass, btnAdd, btnRm)
{
var num = $(divClass).length;
$('#' + divId + num).remove();
//$('#' + btnAdd).attr('disabled','');
if (num-1 == 1) {
$('#' + btnRm).attr('disabled','disabled');
$('#' + btnAdd).attr('disabled',false);
}
}
<div>
<input type="button" id="btnAdd" class="btn" onclick="dupForm('input', '.clonedInput', 'btnAdd', 'btnDel');" value="Add" />
<input type="button" id="btnDel" class="btn" onclick="rmForm('input', '.clonedInput', 'btnAdd', 'btnDel');" value="Remove" /><br /><br />
</div>
<div id="input1" style="margin-bottom:4px;" class="clonedInput">
<input type="text" class="input-block-level-inputfields" name="product1" id="product1" placeholder="Product Details" />
<input type="text" class="input-block-level-inputfields" name="price1" id="price1" style="width:50px;" placeholder="Price" />
</div>
Issues with the Code:
- It is also clones the value if i enter the value in one field and click on add which i do not want.
- in the Price field, i want to enter the price value and it should get calculated immediately in the div box, i will define later. if i click remove the amount should be deducted immediately
Thanks