Created
October 8, 2021 08:13
-
-
Save kebalicious/fc42dc08309426e25a9a20e7012d765b to your computer and use it in GitHub Desktop.
Dynamic Fields add to form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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--; | |
}) | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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