diff --git a/cookbook/form/form_collections.rst b/cookbook/form/form_collections.rst index e5fa49f06b0..c62ab281014 100755 --- a/cookbook/form/form_collections.rst +++ b/cookbook/form/form_collections.rst @@ -358,27 +358,29 @@ will be show next): .. code-block:: javascript - // Get the ul that holds the collection of tags - var collectionHolder = $('ul.tags'); + var $collectionHolder; // setup an "add a tag" link var $addTagLink = $('Add a tag'); var $newLinkLi = $('
').append($addTagLink); jQuery(document).ready(function() { + // Get the ul that holds the collection of tags + $collectionHolder = $('ul.tags'); + // add the "add a tag" anchor and li to the tags ul - collectionHolder.append($newLinkLi); + $collectionHolder.append($newLinkLi); // count the current form inputs we have (e.g. 2), use that as the new // index when inserting a new item (e.g. 2) - collectionHolder.data('index', collectionHolder.find(':input').length); + $collectionHolder.data('index', $collectionHolder.find(':input').length); $addTagLink.on('click', function(e) { // prevent the link from creating a "#" on the URL e.preventDefault(); // add a new tag form (see next code block) - addTagForm(collectionHolder, $newLinkLi); + addTagForm($collectionHolder, $newLinkLi); }); }); @@ -393,22 +395,22 @@ one example: .. code-block:: javascript - function addTagForm(collectionHolder, $newLinkLi) { + function addTagForm($collectionHolder, $newLinkLi) { // Get the data-prototype explained earlier - var prototype = collectionHolder.data('prototype'); + var prototype = $collectionHolder.data('prototype'); // get the new index - var index = collectionHolder.data('index'); + var index = $collectionHolder.data('index'); // Replace '$$name$$' in the prototype's HTML to // instead be a number based on how many items we have var newForm = prototype.replace(/\$\$name\$\$/g, index); // increase the index with one for the next item - collectionHolder.data('index', index + 1); + $collectionHolder.data('index', index + 1); // Display the form in the page in an li, before the "Add a tag" link li - var $newFormLi = $('').append(newForm); + var $newFormLi = $('').append($newForm); $newLinkLi.before($newFormLi); }