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