In this example user will have facility to add more input fields in form dynamically similarly user will also have facility to remove dynamic input fields so once user click remove link it removes the current input field by using its corresponding parent div element.Using this script user can add as much input field up to the maximum number of limit count.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
var maxnum = 5;
var wrapper = $(".inputwrap"); //Fields wrapper
var addlink = $("#addlink"); //Add button ID
var x = 1; //starting count
$(addlink).click(function(e){ //on add input button click
if(x < maxnum){
x++; //text box increment
$(wrapper).append('<div>Text'+x+'<input type="text" name="myinput['+x+'][text1]"/><a href="#" class="remove">Remove</a></div>'); //add input box
}
else{
alert('You have limit of 5 input Fields');
}
});
$(wrapper).on("click",".remove", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</script>
<div class="inputwrap">
<a href="#" id="addlink">Add</a>
<div>Text1<input name="myinput[]" type="text"></div>
</div>
0 Comment(s)