Skip to content

Commit 6145217

Browse files
committed
Merge pull request #2158 from WouterJ/issue_1382
[Cookbook/Form] Fixed jQuery bug
2 parents 3f27c94 + ee6739d commit 6145217

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

cookbook/form/form_collections.rst

Lines changed: 12 additions & 5 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();
@@ -391,21 +395,24 @@ one example:
391395
392396
function addTagForm(collectionHolder, $newLinkLi) {
393397
// Get the data-prototype explained earlier
394-
var prototype = collectionHolder.attr('data-prototype');
398+
var prototype = collectionHolder.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.data('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);
405412
$newLinkLi.before($newFormLi);
406413
}
407414
408-
.. note:
415+
.. note::
409416

410417
It is better to separate your javascript in real JavaScript files than
411418
to write it inside the HTML as is done here.

0 commit comments

Comments
 (0)