Sorry to post such an old and answered question. I had a similar problem and this answer helped me, but they didn't deliver me. After trial and error ... Here is my practical solution for adding areas to a web page and deleting them. I use it with dynamic form fields, but your needs may vary.
Here is the part of the form in the static part of the page.
<fieldset> <legend>Email-tauluun</legend> <a class="button_ajax email-add">Add new area</a> <p> <span class="email_original_area"> <label>Email: <input type="text" name="emails[]" id="email0" /> </label> </span> <span id="email_add_area"></span> </p> </fieldset>
Then we need some JavaScript features. They use jQuery.
<script type="text/javascript"> //we need a counter for dynamic fields var cnt=0; $(document).ready(function(){ $('.email-add').click(function(){ cnt += 1; //let count... $('#email_add_area').append( '<span class="dynExtra"><br /><label>Email: ' + '<input type="text" name="emails[]" id="email' + cnt + '" /></label>' + '</span>' ); //this function creates a button for newly created area //must be done after the creation of the area addRemover(); }); }); function addRemover() { //appends remove button to the last created area $('<a class="button_ajax dynExtra-clear" name="dynExtra-clear">Remove area ' + cnt + '</a>').appendTo('
Hope this helps someone and I haven't forgotten anything.
Zz-bb source share