(function($){

    var settings = {};

    var methods = {

        init : function(options)
        {
            var key = this.selector;

            settings[key] = $.extend( {
                addSelector: '[data-addfield="collection"]',
                removeSelector: '[data-removefield="collection"]',
                wrapperSelector: 'ul.bc-collection',
                elementWrapperTag: 'li'
            }, options);

            return this.each(function(index, element) {

                var $element = $(element);

                $($element, document).on('click', settings[key]['addSelector'], function(event) {

                    event.preventDefault();

                    $link = $(event.target);

                    var $collection = $('#'+ $link.attr('data-collection'));
                    var $wrapper = $collection.find(settings[key]['wrapperSelector']);
                    var count = $wrapper.find(settings[key]['elementWrapperTag']).size();

                    var newWidget = $link.attr('data-prototype') ? $link.attr('data-prototype') : $collection.attr('data-prototype');

                    // Check if an element with this ID already exists.
                    // If it does, increase the count by one and try again
                    var newName = newWidget.match(/id="(.*?)"/);
                    while ($('#' + newName[1].replace(/__name__/g, count)).size() > 0) {
                        count++;
                    }
                    newWidget = newWidget.replace(/__name__/g, count);
                    newWidget = newWidget.replace(/__id__/g, newName[1].replace(/__name__/g, count));

                    var elementWrapper = document.createElement(settings[key]['elementWrapperTag']);
                    elementWrapper.innerHTML = newWidget;

                    $.when(
                        $(elementWrapper).appendTo($wrapper)
                    ).done(function() {
                        $(document).trigger('saxulum-collection.add', [$(settings[key]['elementWrapperTag'], $wrapper).last()]);
                    });
                });

                $($element, document).on('click', settings[key]['removeSelector'], function(event) {

                    event.preventDefault();

                    $link = $(event.target);

                    var $widget = $('#'+ $link.attr('data-field'));
                    var $elementWrapper = false;

                    $widget.parents(settings[key]['elementWrapperTag']).each(function(i, element){
                        if(!$elementWrapper) {
                            $element = $(element);
                            if($element.parent(settings[key]['wrapperSelector'])) {
                                $elementWrapper = $element;
                            }
                        }
                    });

                    if(typeof $elementWrapper != null) {
                        $.when(
                            $(document).trigger('saxulum-collection.remove', [$elementWrapper])
                        ).done(function() {
                            $elementWrapper.remove();
                        });
                    }
                });
            });
        }
    };

    $.fn.saxulumCollection = function(method) {
        if ( methods[method] ) {
            return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jquery.saxulum-collection.js' );
        }
    };

})(jQuery);
@@ -394,16 +394,84 @@
 ag: 'li'
+,%0A                elementWrapperAttribute: 'data-collection-element'
 %0A       
@@ -985,20 +985,24 @@
 wrapper.
-find
+children
 (setting
@@ -1829,35 +1829,215 @@
     
-elementWrapper.innerHTML = 
+var $elementWrapper = $(elementWrapper);%0A%0A                    $elementWrapper.attr(settings%5Bkey%5D%5B'elementWrapperAttribute'%5D, newName%5B1%5D.replace(/__name__/g, count));%0A                    $elementWrapper.html(
 newW
@@ -2041,16 +2041,17 @@
 ewWidget
+)
 ;%0A%0A     
@@ -2098,17 +2098,16 @@
        $
-(
 elementW
@@ -2112,17 +2112,16 @@
 tWrapper
-)
 .appendT
@@ -2568,584 +2568,155 @@
 ar $
-widget = $('#'+ $link.attr('data-field'));%0A                    var $elementWrapper = false;%0A%0A                    $widget.parents(settings%5Bkey%5D%5B'elementWrapperTag'%5D).each(function(i, element)%7B%0A                        if(!$elementWrapper) %7B%0A                            $element = $(element);%0A                            if($element.parent(settings%5Bkey%5D%5B'wrapperSelector'%5D)) %7B%0A                                $elementWrapper = $element;%0A                            %7D%0A                        %7D%0A                    %7D);%0A%0A                    if(typeof $elementWrapper != null) %7B%0A    
+elementWrapper = $(settings%5Bkey%5D%5B'elementWrapperTag'%5D + '%5B' + settings%5Bkey%5D%5B'elementWrapperAttribute'%5D + '=%22' + $link.attr('data-field') + '%22%5D');%0A%0A
     
@@ -2731,36 +2731,32 @@
         $.when(%0A
-    
                 
@@ -2843,36 +2843,32 @@
                 
-    
 ).done(function(
@@ -2863,36 +2863,32 @@
 ne(function() %7B%0A
-    
                 
@@ -2945,37 +2945,11 @@
     
-    %7D);%0A                    %7D
+%7D);
 %0A   

enhance script, remove the element selected by element selector