Advertisement
linuxatico

Untitled

May 16th, 2012
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Get the div that holds the collection of tags
  2. var collectionHolder = $('ul.tags');
  3.  
  4. // setup an "add a tag" link
  5. var $addTagLink = $('<a href="#" class="add_tag_link">Add a tag</a>');
  6. var $newLinkLi = $('<li></li>').append($addTagLink);
  7.  
  8. jQuery(document).ready(function() {
  9.     // add the "add a tag" anchor and li to the tags ul
  10.     collectionHolder.append($newLinkLi);
  11.  
  12.     $addTagLink.on('click', function(e) {
  13.         // prevent the link from creating a "#" on the URL
  14.         e.preventDefault();
  15.  
  16.         // add a new tag form (see next code block)
  17.         addTagForm(collectionHolder, $newLinkLi);
  18.     });
  19. });
  20.  
  21.  
  22. function addTagForm(collectionHolder, $newLinkLi) {
  23.     // Get the data-prototype we explained earlier
  24.     var prototype = collectionHolder.attr('data-prototype');
  25.  
  26.     // Replace '$$name$$' in the prototype's HTML to
  27.     // instead be a number based on the current collection's length.
  28.     var newForm = prototype.replace(/\$\$name\$\$/g, collectionHolder.children().length);
  29.  
  30.     // Display the form in the page in an li, before the "Add a tag" link li
  31.     var $newFormLi = $('<li></li>').append(newForm);
  32.     $newLinkLi.before($newFormLi);
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement