Why does cloning the django inline formsets result to forms with similar ids and names?
Posted
by
user1289167
on Stack Overflow
See other posts from Stack Overflow
or by user1289167
Published on 2012-05-30T09:43:32Z
Indexed on
2012/05/30
16:40 UTC
Read the original article
Hit count: 139
In my project I use django inline formsets. I got the jquery to clone the formsets but unfortunately the cloned forms have the same names and ids and so data entered in the last one overwrites the data from the first form. What could I be doing wrong?
Here is the script:
<script type="text/javascript">>
function cloneMore(selector, type) {
var newElement = $(selector).clone(true);
var total = $('#id_' + type + '-TOTAL_FORMS').val();
newElement.find(':input').each(function() {
var name = $(this).attr('name').replace('-' + (total-1) + '-','-' + total + '-');
var id = 'id_' + name;
$(this).attr({'name': name, 'id': id}).val('').removeAttr('checked');
});
newElement.find('label').each(function() {
var newFor = $(this).attr('for').replace('-' + (total-1) + '-','-' + total + '-');
$(this).attr('for', newFor);
});
total++;
$('#id_' + type + '-TOTAL_FORMS').val(total);
$(selector).after(newElement);
}
</script>
© Stack Overflow or respective owner