I have this Code for add more input using jQuery:
JS:
$(document).ready(function () {
var MaxInputs = 8; //maximum input boxes allowed
var InputsWrapper = $("#InputsWrapper"); //Input boxes wrapper ID
var AddButton = $("#AddMoreFileBox"); //Add button ID
var x = InputsWrapper.length; //initlal text box count
var FieldCount = 1; //to keep track of text box added
$(AddButton).click(function (e) //on add input button click
{
if (x <= MaxInputs) //max input box allowed
{
FieldCount++; //text box added increment
//add input box
$(InputsWrapper).append('<div><input type="text" name="mytext[]" id="field" value="Text ' + FieldCount + '"/><input type="text" name="mytext1[]" id="field" value="Text ' + FieldCount + '"/><a href="#" class="removeclass">×</a></div>');
x++; //text box increment
}
return false;
});
$("body").on("click", ".removeclass", function (e) { //user click on remove text
if (x > 1) {
$(this).parent('div').remove(); //remove text box
x--; //decrement textbox
}
return false;
})
});
HTML:
<a href="#" id="AddMoreFileBox" class="btn btn-info">Add More Field</a>
<div id="InputsWrapper">
<div>
<input type="text" name="mytext1[]" id="field" value="Text 1"><a href="#" class="removeclass">×</a>
</div>
<div>
<input type="text" name="mytext1[]" id="field" value="Text 1"><a href="#" class="removeclass">×</a>
</div>
</div>
in default i add two input and set 8 input for MaxInput ans set 1 for keep input.
Now, I have Two Problem:
1- i cant remove One of these two default input.
2- Maxinput not work with +1 default input. my mean when we set 1 keep input, if add One default Max input This not work and add 8 input + 1 defualt + 1 keep input. with this we have 10 input. this is false. we need to add 9 input.
How do can can i fix this problems?
Live Demo : http://jsfiddle.net/5UCex/1/