Skip to content

Instantly share code, notes, and snippets.

@tradesouthwest
Created June 10, 2019 03:29
Show Gist options
  • Save tradesouthwest/afa121f03d506fd6f43b1cb6dc645a5d to your computer and use it in GitHub Desktop.
Save tradesouthwest/afa121f03d506fd6f43b1cb6dc645a5d to your computer and use it in GitHub Desktop.
$(document).ready(function(){
var maxField = 10; //Input fields increment limitation
var addButton = $('.add_button'); //Add button selector
var wrapper = $('.field_wrapper'); //Input field wrapper
var fieldHTML = '<div><input type="text" name="field_name[]" value=""/><a href="javascript:void(0);" class="remove_button"><img src="remove-icon.png"/></a></div>'; //New input field html
var x = 1; //Initial field counter is 1
//Once add button is clicked
$(addButton).click(function(){
//Check maximum number of input fields
if(x < maxField){
x++; //Increment field counter
$(wrapper).append(fieldHTML); //Add field html
}
});
//Once remove button is clicked
$(wrapper).on('click', '.remove_button', function(e){
e.preventDefault();
$(this).parent('div').remove(); //Remove field html
x--; //Decrement field counter
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment