Last active
August 29, 2015 14:06
-
-
Save anyt/97fee311730283b93371 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$collectionHolder = $('#tags'); | |
$addTagLink = $('#add-tag'); | |
function addTagForm($collectionHolder, ) { | |
// Get the data-prototype explained earlier | |
var prototype = $collectionHolder.data('prototype'); | |
// get the new 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); | |
// Display the form in the page in an li, before the "Add a tag" link li | |
var $newFormLi = $('<li></li>').append(newForm); | |
$collectionHolder.append($newformLi); | |
} | |
$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); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul id="tags" data-prototype="<div><label class=" required">__name__</label><div id="task_tags___name__"><div><label for="task_tags___name___name" class=" required">Name</label><input type="text" id="task_tags___name___name" name="task[tags][__name__][name]" required="required" maxlength="255" /></div></div></div>"> | |
<a id="add-tag" href="#">Add tag</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment