@@ -351,27 +351,29 @@ will be show next):
351351
352352.. code-block :: javascript
353353
354- // Get the ul that holds the collection of tags
355- var collectionHolder = $ (' ul.tags' );
354+ var $collectionHolder;
356355
357356 // setup an "add a tag" link
358357 var $addTagLink = $ (' <a href="#" class="add_tag_link">Add a tag</a>' );
359358 var $newLinkLi = $ (' <li></li>' ).append ($addTagLink);
360359
361360 jQuery (document ).ready (function () {
361+ // Get the ul that holds the collection of tags
362+ $collectionHolder = $ (' ul.tags' );
363+
362364 // add the "add a tag" anchor and li to the tags ul
363- collectionHolder .append ($newLinkLi);
365+ $ collectionHolder .append ($newLinkLi);
364366
365367 // count the current form inputs we have (e.g. 2), use that as the new
366368 // index when inserting a new item (e.g. 2)
367- collectionHolder .data (' index' , collectionHolder .find (' :input' ).length );
369+ $ collectionHolder .data (' index' , $ collectionHolder .find (' :input' ).length );
368370
369371 $addTagLink .on (' click' , function (e ) {
370372 // prevent the link from creating a "#" on the URL
371373 e .preventDefault ();
372374
373375 // add a new tag form (see next code block)
374- addTagForm (collectionHolder, $newLinkLi);
376+ addTagForm ($ collectionHolder, $newLinkLi);
375377 });
376378 });
377379
@@ -389,22 +391,22 @@ one example:
389391
390392.. code-block :: javascript
391393
392- function addTagForm (collectionHolder , $newLinkLi ) {
394+ function addTagForm ($ collectionHolder , $newLinkLi ) {
393395 // Get the data-prototype explained earlier
394- var prototype = collectionHolder .data (' prototype' );
396+ var prototype = $ collectionHolder .data (' prototype' );
395397
396398 // get the new index
397- var index = collectionHolder .data (' index' );
399+ var index = $ collectionHolder .data (' index' );
398400
399401 // Replace '__name__' in the prototype's HTML to
400402 // instead be a number based on how many items we have
401403 var newForm = prototype .replace (/ __name__/ g , index);
402404
403405 // increase the index with one for the next item
404- collectionHolder .data (' index' , index + 1 );
406+ $ collectionHolder .data (' index' , index + 1 );
405407
406408 // Display the form in the page in an li, before the "Add a tag" link li
407- var $newFormLi = $ (' <li></li>' ).append (newForm);
409+ var $newFormLi = $ (' <li></li>' ).append ($ newForm);
408410 $newLinkLi .before ($newFormLi);
409411 }
410412
0 commit comments