Skip to content

Instantly share code, notes, and snippets.

@kebalicious
Created October 8, 2021 08:13
Show Gist options
  • Save kebalicious/fc42dc08309426e25a9a20e7012d765b to your computer and use it in GitHub Desktop.
Save kebalicious/fc42dc08309426e25a9a20e7012d765b to your computer and use it in GitHub Desktop.
Dynamic Fields add to form
<div class="input_fields_wrap">
<button class="add_field_button">Add More Fields</button>
<div>
<input type="text" name="mytext[1]">
<input type="date" name="mydate[1]">
<select name="myselect[1]"><option>Please Select</option></select>
</div>
</div>
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div> <input type="text" name="mytext[' + x + ']"/> <input type="date" name="mydate[' + x + ']"/> <select name="myselect[' + x + ']"><option>Please Select</option></select> <a href="#" class="remove_field">Remove</a> </div>'); // add input boxes.
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment