Problem using jQuery to add a cloned form dynamically
Hi everyone,i have a form and an add new form link that adds a multiple form like the first one dynamically, and inside the form there is an author text input with also a link to add dynamically multiple author's input.
the problem is that when i add multiple forms..only in the first form i can add multiple authors, and not in the other forms. and the author's text inputs are added outside the first form.
Can somebody help me identifying the probleme in my code, please.
Also can somebody informe me how ca i add a delete link to delete each form. Thank you.
Here's my code :
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Untitled Document</title>
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
- <script>
- $(document).ready(function() {
- var MaxInputs = 8; //maximum input boxes allowed
- var InputsWrapper = $("#InputsWrapper"); //Input boxes wrapper ID
- var AddForm = $("#AddMoreForm"); //Add button ID
- var AddAuthor = $("#AddMoreAuthor"); //Add button ID
- var x = InputsWrapper.length; //initlal text box count
- var FieldCount=1; //to keep track of text box added
- $(AddForm).click(function (e) //on add input button click
- {
- if(x <= MaxInputs) //max input box allowed
- {
- FieldCount++; //text box added increment
- //add input box
- $(InputsWrapper).append('<div><form style="border:3px double #F7730E;" name="forms[]" id="form_'+ FieldCount +'" >Titre:<input type="text" name="Titre[]"><div>Auteur:<input type="text" name="myAuteur[]" id="Auteur_1" value="Auteur 1" /><a href="#" class="removeclass">×</a></div><a href="#" id="AddMoreAuthor" class="btn btn-info">Add More Auteur</a></div><input type="submit" value="Ajouter publication" /></form></div>');
- x++; //text box increment
- }
- return false;
- });
- //button d'ajout d'auteur
- $(AddAuthor).click(function (e) //on add auteur input button click
- {
- if(x <= MaxInputs) //max input box allowed
- {
- FieldCount++; //text box added increment
- //add input box
- $(InputsWrapper).append('<div>Auteur:<input type="text" name="myAuteur[]" id="Auteur_'+ FieldCount +'" value="Auteur '+ FieldCount +'"/><a href="#" class="removeclass">×</a></a></div>');
- x++; //text box incrementb
- }
- return false;
- });
- $("body").on("click",".removeclass", function(e){ //user click on remove text
- if( x > 1 ) {
- $(this).parent('div').remove(); //remove text box
- x--; //decrement textbox
- }
- return false;
- })
- });
- </script>
- </head>
- <body>
- <a href="#" id="AddMoreForm" class="btn btn-info">Add More Form</a>
- <div id="InputsWrapper">
- <div><form style="border:3px double #F7730E;" name="forms[]" id="form_1" >Titre:<input type="text" name="Titre[]">
- <div>Auteur:<input type="text" name="myAuteur[]" id="Auteur_1" value="Auteur 1"><a href="#" class="removeclass">×</a><a href="#" id="AddMoreAuthor" class="btn btn-info" onclick="">Add More Authors</a></div>
- <input type="submit" value="Ajouter publication" />
- </form></div>
- </div>
- </body>
- </html>