Note that there are some explanatory texts on larger screens.

plurals
  1. POMutilple javascript variable iterations with jquery
    primarykey
    data
    text
    <p>i have a form and would like to give users the ability to duplicate a group of fields as many times as necessary. With one group it iterates correctly but when I add a second group the "current" variable iterates collectively instead of being unique to each group... i tried changing all of the "current" to "newFieldset.current" but that returns NAN... any ideas?</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function() { var current = 0; //Add New Fieldset with Button var newFieldset = { init: function(groupIndex) { current++; $newPerson= $("#Template"+groupIndex).clone(true); $newPerson.children("p").children("label").each(function(i) { var $currentElem= $(this); $currentElem.attr("for",$currentElem.attr("for")+current); }); $newPerson.children("p").children("input").each(function(i) { var $currentElem= $(this); $currentElem.attr("name",$currentElem.attr("name")+current); $currentElem.attr("value",$currentElem.attr("value")+groupIndex+current); $currentElem.attr("id",$currentElem.attr("id")+current); }); $newPerson.appendTo("#mainField"+groupIndex); $newPerson.removeClass("hideElement"); }, currentID: null, obj: null }; $(".addButton").each(function() { $(this).click(function() { var groupIndex = $(this).attr("title"); //newFieldset.obj = this; //var fieldIndex = $(this).attr("class"); newFieldset.init(groupIndex); }); }); console.log('r'); }); &lt;/script&gt; &lt;style&gt; .hideElement {display:none;} &lt;/style&gt; &lt;form name="demoForm" id="demoForm" method="post" action="#"&gt; &lt;div id="groupCtr1"&gt; &lt;fieldset id="mainField1"&gt; &lt;div id="Template1" class="hideElement"&gt; &lt;p&gt; &lt;label for="firstname"&gt;Name&lt;/label&gt; &lt;em&gt;*&lt;/em&gt; &lt;input id="firstname" name="firstname" size="25" /&gt; &lt;input id="lastname" name="lastname" size="25" /&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="email"&gt;Email&lt;/label&gt; &lt;em&gt;*&lt;/em&gt;&lt;input id="email" name="email" size="25" /&gt; &lt;/p&gt; &lt;/div&gt; &lt;div&gt; &lt;p&gt; &lt;label for="firstname1"&gt;Name&lt;/label&gt; &lt;em&gt;*&lt;/em&gt; &lt;input id="firstname1" name="firstname1" size="25" /&gt; &lt;input id="lastname1" name="lastname1" size="25" /&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="email1"&gt;Email&lt;/label&gt; &lt;em&gt;*&lt;/em&gt;&lt;input id="email1" name="email1" size="25" /&gt; &lt;/p&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;p&gt; &lt;input type="button" class="addButton" title="1" value="Add Another Person"&gt; &lt;/p&gt; &lt;/div&gt; &lt;div id="groupCtr2"&gt; &lt;fieldset id="mainField2"&gt; &lt;div id="Template2" class="hideElement"&gt; &lt;p&gt; &lt;label for="coname"&gt;Company Name&lt;/label&gt; &lt;em&gt;*&lt;/em&gt; &lt;input id="coname" name="coname" size="25" /&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="codesc"&gt;Description&lt;/label&gt; &lt;em&gt;*&lt;/em&gt;&lt;input id="codesc" name="codesc" size="25" /&gt; &lt;/p&gt; &lt;/div&gt; &lt;div&gt; &lt;p&gt; &lt;label for="coname1"&gt;Company Name&lt;/label&gt; &lt;em&gt;*&lt;/em&gt; &lt;input id="coname1" name="coname1" size="25" /&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="codesc1"&gt;Description&lt;/label&gt; &lt;em&gt;*&lt;/em&gt;&lt;input id="codesc1" name="codesc1" size="25" /&gt; &lt;/p&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;p&gt; &lt;input type="button" class="addButton" title="2" value="Add Another Company"&gt; &lt;/p&gt; &lt;/div&gt; &lt;input type="submit" value="Save"&gt; &lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload