@@ -369,6 +369,10 @@ will be show next):
369
369
// add the "add a tag" anchor and li to the tags ul
370
370
collectionHolder .append ($newLinkLi);
371
371
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
+
372
376
$addTagLink .on (' click' , function (e ) {
373
377
// prevent the link from creating a "#" on the URL
374
378
e .preventDefault ();
@@ -393,12 +397,15 @@ one example:
393
397
// Get the data-prototype explained earlier
394
398
var prototype = collectionHolder .attr (' data-prototype' );
395
399
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 ' ) ;
398
402
399
403
// Replace '$$name$$' in the prototype's HTML to
400
404
// 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 );
402
409
403
410
// Display the form in the page in an li, before the "Add a tag" link li
404
411
var $newFormLi = $ (' <li></li>' ).append (newForm);
0 commit comments