Skip to content

Commit 3ab0a5c

Browse files
committed
[#1920] Modifying the fix to form_collection - previous we were counting the children, which included the child elements *and* the single "Add a tag" link. Subtracting 1 was correct, but wasn't clear. Counting the number of actual form elements is more straightforward, and then we don't need to subtract 1.
1 parent e5360fb commit 3ab0a5c

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

cookbook/form/form_collections.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,12 @@ one example:
393393
// Get the data-prototype explained earlier
394394
var prototype = collectionHolder.attr('data-prototype');
395395
396+
// count the current form inputs we have (e.g. 2), use that as the new index (e.g. 2)
397+
var newIndex = collectionHolder.find(':input').length;
398+
396399
// Replace '$$name$$' in the prototype's HTML to
397-
// instead be a number based on the current collection's length.
398-
var newForm = prototype.replace(/\$\$name\$\$/g, collectionHolder.children().length - 1);
400+
// instead be a number based on how many items we have
401+
var newForm = prototype.replace(/\$\$name\$\$/g, newIndex);
399402
400403
// Display the form in the page in an li, before the "Add a tag" link li
401404
var $newFormLi = $('<li></li>').append(newForm);

0 commit comments

Comments
 (0)