Skip to content

Instantly share code, notes, and snippets.

@jokull
Created April 28, 2011 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jokull/946188 to your computer and use it in GitHub Desktop.
Save jokull/946188 to your computer and use it in GitHub Desktop.
Appends an `add` button to all `.multiple-items` rendered by fungiform for adding `Multiple` or `MultiChoiceField` in the DOM
(function(){
var resetLi = function($li, index){
$li.find(":input, label").each(function(i){
var $that = $(this);
$.each(['name', 'id', 'for'], function(i, attribute){
var value = $that.attr(attribute);
if(value){
$that.attr(attribute, value.replace(/\d+/, index))
}
});
$that.val("");
});
return $li;
};
$(".multiple-items").each(function(){
var $parent = $(this),
$add = $('<a href="#" class="add">Add</a>');
$add.click(function(e){
e.preventDefault();
var $li = $parent.find("li:last").clone(),
name = $(':input[name]', $li).attr('name');
if(name.search(/\d+/)>-1){
var index = Number(name.match(/\d+/)[0]);
$parent.append(resetLi($li, index + 1));
}
});
$parent.after($add);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment