Skip to content

Commit c9b1cb4

Browse files
committed
[symfony#1382] Fixed jQuery bug
1 parent 95582d6 commit c9b1cb4

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

cookbook/form/form_collections.rst

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ will be show next):
369369
// add the "add a tag" anchor and li to the tags ul
370370
collectionHolder.append($newLinkLi);
371371
372+
// count the current form inputs we have (e.g. 2), use that as the new
373+
// index when inserting a new item (e.g. 2)
374+
collectionHolder.data('index', collectionHolder.find(':input').length);
375+
372376
$addTagLink.on('click', function(e) {
373377
// prevent the link from creating a "#" on the URL
374378
e.preventDefault();
@@ -393,12 +397,15 @@ one example:
393397
// Get the data-prototype explained earlier
394398
var prototype = collectionHolder.attr('data-prototype');
395399
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;
400+
// get the new index
401+
var index = collectionHolder.data('index');
398402
399403
// Replace '$$name$$' in the prototype's HTML to
400404
// instead be a number based on how many items we have
401-
var newForm = prototype.replace(/\$\$name\$\$/g, newIndex);
405+
var newForm = prototype.replace(/\$\$name\$\$/g, index);
406+
407+
// increase the index with one for the next item
408+
collectionHolder.attr('index', index + 1);
402409
403410
// Display the form in the page in an li, before the "Add a tag" link li
404411
var $newFormLi = $('<li></li>').append(newForm);

0 commit comments

Comments
 (0)