In today’s era, we website designers and developers needs to add some text area like repeater fields into our webpage. If you are also searching for such a tutorial or code then you are at right place. You can use the codes into your projects and website. It is free to use. Go ahead to the get the step by step code. If you just want the complete code then you can go ahead to the end of this tutorial.
Step 1:
First of all, we have to add the script and style-sheet for bootstrap and jquery implementation. So add the following code into <head> tag of you page
1 2 3 | <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> |
step 2:
Now add the script and some styles into head section again.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <style type="text/css"> * { .border-radius(0) !important; } #field { margin-bottom:20px; } .input { width: 96%; float: left; margin-right: 5px; } </style> <script type="text/javascript"> $(document).ready(function(){ var next = 1; $(".add-more").click(function(e){ e.preventDefault(); var addto = "#field" + next; var addRemove = "#field" + (next); next = next + 1; var newIn = '<input autocomplete="off" class="input form-control" id="field' + next + '" name="field' + next + '" type="text">'; var newInput = $(newIn); var removeBtn = '<button id="remove' + (next - 1) + '" class="btn btn-danger remove-me" >-</button></div><div id="field">'; var removeButton = $(removeBtn); $(addto).after(newInput); $(addRemove).after(removeButton); $("#field" + next).attr('data-source',$(addto).attr('data-source')); $("#count").val(next); $('.remove-me').click(function(e){ e.preventDefault(); var fieldNum = this.id.charAt(this.id.length-1); var fieldID = "#field" + fieldNum; $(this).remove(); $(fieldID).remove(); }); }); }); </script> |
Step 3:
Now add the following html code into <body> tag.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <div class="container"> <div class="row"> <div class="col-sm-12"> <input type="hidden" name="count" value="1" /> <div class="control-group" id="fields"> <label class="control-label" for="field1">Dynamically Add/Remove Fields</label> <div class="controls" id="profs"> <form class="input-append"> <div id="field"> <input autocomplete="off" class="dcs_field input form-control" id="field1" name="prof1" type="text" placeholder="Type something" data-items="8"/> <button id="b1" class="btn add-more" type="button">+</button> </div> </form> <br> <small>Press + to add another form field :)</small> </div> </div> </div> </div> </div> |
Complete Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | <html> <head> <title>Code To Dynamically add and remove fields with jquery</title> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <style type="text/css"> * { .border-radius(0) !important; } #field { margin-bottom:20px; } .input { width: 96%; float: left; margin-right: 5px; } </style> <script type="text/javascript"> $(document).ready(function(){ var next = 1; $(".add-more").click(function(e){ e.preventDefault(); var addto = "#field" + next; var addRemove = "#field" + (next); next = next + 1; var newIn = '<input autocomplete="off" class="input form-control" id="field' + next + '" name="field' + next + '" type="text">'; var newInput = $(newIn); var removeBtn = '<button id="remove' + (next - 1) + '" class="btn btn-danger remove-me" >-</button></div><div id="field">'; var removeButton = $(removeBtn); $(addto).after(newInput); $(addRemove).after(removeButton); $("#field" + next).attr('data-source',$(addto).attr('data-source')); $("#count").val(next); $('.remove-me').click(function(e){ e.preventDefault(); var fieldNum = this.id.charAt(this.id.length-1); var fieldID = "#field" + fieldNum; $(this).remove(); $(fieldID).remove(); }); }); }); </script> </head> <body> <div class="container"> <div class="row"> <div class="col-sm-12"> <input type="hidden" name="count" value="1" /> <div class="control-group" id="fields"> <label class="control-label" for="field1">Dynamically Add/Remove Fields</label> <div class="controls" id="profs"> <form class="input-append"> <div id="field"> <input autocomplete="off" class="dcs_field input form-control" id="field1" name="prof1" type="text" placeholder="Type something" data-items="8"/> <button id="b1" class="btn add-more" type="button">+</button> </div> </form> <br> <small>Press + to add another form field :)</small> </div> </div> </div> </div> </div> </body> </html> |