Reputation: 2961
One types the number of Inputs they want and click GO, it creates that many number of input fields. How do I make it so each will have their own unique name?
I want to have unique names for each input created so ie:
2nd Inputs: name="productcode2"
name="desc2" name="dollars2" name="cents2"
$(document).ready(function(){
$('#addButton').click(function(){
var count = parseInt($('#quantity').val());
var newHTML = [];
for(var i=0;i<count;i++){
newHTML.push('Product Code: <input name="productcode[]" type="text"/> Description: <input name="desc[]" type="text"/> $<input width="100px" type="text" name="dollars[]" size="5" maxlength="6"/>.<input width="50px" type="text" name="cents[]" size="1" maxlength="2"/><br/>');
}
$('#sandbox').html(newHTML.join(''));
});
});
FYI I'm also using PhP if it is easier to be incorporated
Upvotes: 1
Views: 1216
Reputation: 146320
You can what is on this fiddle:
$(document).ready(function(){
var numberOfInputs = 0;
$('#addButton').click(function(){
var count = parseInt($('#quantity').val());
var newHTML = [];
for(var i=0;i<count;i++){
var html = $('Product Code: <input name="productcode" type="text"/> Description: <input name="desc" type="text"/> $<input width="100px" type="text" name="dollars" size="5" maxlength="6"/>.<input width="50px" type="text" name="cents" size="1" maxlength="2"/><br/>');
$(html[0]).attr('name',$(html[0]).attr('name')+numberOfInputs);
$(html[2]).attr('name',$(html[2]).attr('name')+numberOfInputs);
$(html[4]).attr('name',$(html[4]).attr('name')+numberOfInputs);
$(html[6]).attr('name',$(html[6]).attr('name')+numberOfInputs);
numberOfInputs++
newHTML.push(html);
$('#sandbox').append(html);
}
});
});
Upvotes: 0