Created
April 28, 2011 11:34
-
-
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
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
(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