On 4/2/2016 6:20 PM, Denis Makarov wrote:
> Hello! I have a MyForm which have name, quantity and quandesc fields.
>
> I have a basic UI form for adding this fileld.
>
> I added button for adding another instances of field on web page.
> Jquery func below:
>
> |
> functionaddContainer(){
> $("#name").clone().appendTo("#food-container");
> $("#quantity").clone().appendTo("#food-container");
> $("#quandesc").clone().appendTo("#food-container");
> }
> |
>
This gives you duplicate IDs. IDs in HTML should be unique. It appears
that the 1st occurrence of an ID is ignored, since you are getting the
value of the 2nd.
To solve this you must generate a new (unique) ID each time you clone a
field. I suggest you maintain a counter that is incremented by 1 each
time you run addContainer. Append this counter to the original ID.
Something like (I don't know the precise syntax)
|ctr = ctr + 1;
field = $("#name").clone();
field['id']="name"+ctr; ?? not sure if this is the way to set the ID
field.appendTo("#food-container");
ditto for the other 2 fields.
While you are at it how about making an array of the 3 original fields,
then applying the code to each element of that array.
Bob Gailer|