@@ -358,27 +358,29 @@ will be show next):
358
358
359
359
.. code-block :: javascript
360
360
361
- // Get the ul that holds the collection of tags
362
- var collectionHolder = $ (' ul.tags' );
361
+ var $collectionHolder;
363
362
364
363
// setup an "add a tag" link
365
364
var $addTagLink = $ (' <a href="#" class="add_tag_link">Add a tag</a>' );
366
365
var $newLinkLi = $ (' <li></li>' ).append ($addTagLink);
367
366
368
367
jQuery (document ).ready (function () {
368
+ // Get the ul that holds the collection of tags
369
+ $collectionHolder = $ (' ul.tags' );
370
+
369
371
// add the "add a tag" anchor and li to the tags ul
370
- collectionHolder .append ($newLinkLi);
372
+ $ collectionHolder .append ($newLinkLi);
371
373
372
374
// count the current form inputs we have (e.g. 2), use that as the new
373
375
// index when inserting a new item (e.g. 2)
374
- collectionHolder .data (' index' , collectionHolder .find (' :input' ).length );
376
+ $ collectionHolder .data (' index' , $ collectionHolder .find (' :input' ).length );
375
377
376
378
$addTagLink .on (' click' , function (e ) {
377
379
// prevent the link from creating a "#" on the URL
378
380
e .preventDefault ();
379
381
380
382
// add a new tag form (see next code block)
381
- addTagForm (collectionHolder, $newLinkLi);
383
+ addTagForm ($ collectionHolder, $newLinkLi);
382
384
});
383
385
});
384
386
@@ -393,22 +395,22 @@ one example:
393
395
394
396
.. code-block :: javascript
395
397
396
- function addTagForm (collectionHolder , $newLinkLi ) {
398
+ function addTagForm ($ collectionHolder , $newLinkLi ) {
397
399
// Get the data-prototype explained earlier
398
- var prototype = collectionHolder .data (' prototype' );
400
+ var prototype = $ collectionHolder .data (' prototype' );
399
401
400
402
// get the new index
401
- var index = collectionHolder .data (' index' );
403
+ var index = $ collectionHolder .data (' index' );
402
404
403
405
// Replace '$$name$$' in the prototype's HTML to
404
406
// instead be a number based on how many items we have
405
407
var newForm = prototype .replace (/ \$\$ name\$\$ / g , index);
406
408
407
409
// increase the index with one for the next item
408
- collectionHolder .data (' index' , index + 1 );
410
+ $ collectionHolder .data (' index' , index + 1 );
409
411
410
412
// Display the form in the page in an li, before the "Add a tag" link li
411
- var $newFormLi = $ (' <li></li>' ).append (newForm);
413
+ var $newFormLi = $ (' <li></li>' ).append ($ newForm);
412
414
$newLinkLi .before ($newFormLi);
413
415
}
414
416
0 commit comments