1// add-collection-widget.js
2jQuery(document).ready(function () {
3 jQuery('.add-another-collection-widget').click(function (e) {
4 var list = jQuery(jQuery(this).attr('data-list-selector'));
5 // Try to find the counter of the list or use the length of the list
6 var counter = list.data('widget-counter') || list.children().length;
7
8 // grab the prototype template
9 var newWidget = list.attr('data-prototype');
10 // replace the "__name__" used in the id and name of the prototype
11 // with a number that's unique to your emails
12 // end name attribute looks like name="contact[emails][2]"
13 newWidget = newWidget.replace(/__name__/g, counter);
14 // Increase the counter
15 counter++;
16 // And store it, the length cannot be used if deleting widgets is allowed
17 list.data('widget-counter', counter);
18
19 // create a new list element and add it to the list
20 var newElem = jQuery(list.attr('data-widget-tags')).html(newWidget);
21 newElem.appendTo(list);
22 });
23});
24