Forked from lavoiesl/collections-add-delete-links.js
Last active
December 18, 2015 02:18
-
-
Save lemoinem/5709468 to your computer and use it in GitHub Desktop.
This file contains 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
$(function(){ | |
var addLink = $('<button type="button" class="btn btn-info add"><i class="icon icon-plus icon-white"></i></button>'); | |
var deleteLink = $('<button type="button" class="btn btn-danger remove"><i class="icon icon-trash icon-white"></i></button>'); | |
function addLinkClick(e) { | |
e.preventDefault(); | |
var collection = $(this).closest(".collection-allow-add"); | |
var index = 1+ collection.data("index"); | |
var prototype = collection.data("prototype").replace(/__name__label__/g, index).replace(/__name__/g, index); | |
prototype = $(prototype); | |
if (collection.hasClass("collection-allow-delete")) { | |
prototype.append(deleteLink.clone().click(deleteLinkClick)); | |
} | |
collection.data("index", index); | |
collection.children(".controls").append(prototype); | |
} | |
function deleteLinkClick(e) { | |
e.preventDefault(); | |
$(this).parent().remove(); | |
} | |
$("form .collection-allow-delete").each(function(){ | |
$("> .controls > .collection-item", this).append(deleteLink.clone().click(deleteLinkClick)); | |
}); | |
$("form .collection-allow-add").each(function(){ | |
if (!$(this).data("prototype").length) return; | |
$(this).data("index", $(this).children().length); | |
addLink.click(addLinkClick); | |
$(this).append(addLink); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment