There is such a code to add a formset for the site. You need to add a delete button to each new formset. Accordingly, the buttons will have the same id as the formset. Can you please tell me how to make buttons with different id ?

 $('#add_issueforinvoice').on('click', function(e){ e.preventDefault(); var form_count = $('#id_invoiceforproject_set-TOTAL_FORMS').val(), id = '#invoice_' + (parseInt(form_count)), prev = parseInt(form_count), count = parseInt(form_count) + 1, form_group = $('#invoice_1'); var new_form_group = form_group.clone().prop( 'id', 'invoice_' + count ).insertAfter($(id)); $('#invoice_' + count).find('div').prop('id', function(index, id) { if (this.id) return this.id.replace('set-0-', 'set-' + (count - 1) + '-'); }); $('#invoice_' + count).find('select').prop('id', function(index, id) { if (this.id) return this.id.replace('set-0-', 'set-' + (count - 1) + '-'); }); $('#invoice_' + count).find('select').prop('name', function(index, id) { if (this.name) return this.name.replace('set-0-', 'set-' + (count - 1) + '-'); }); $('#invoice_' + count).find('label').attr('for', function(index, id) { if (this.for) return this.id.replace('set-0-', 'set-' + (count - 1) + '-'); }); $('#invoice_' + count).find('input').prop('name', function(index, id) { if (this.name) return this.name.replace('set-0-', 'set-' + (count - 1) + '-'); }); $('#invoice_' + count).find('input').prop('id', function(index, id) { if (this.id) return this.id.replace('set-0-', 'set-' + (count - 1) + '-'); }); $('#id_invoiceforproject_set-TOTAL_FORMS').val(parseInt(form_count) + 1); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 

  • and why id this button? Immediately add a handler to it, in which you delete the fresh form and everything, no id is needed - Grundy
  • @Grundy, so I need to remove any form not necessarily fresh. Suppose I created 4 formsets using this code, and then I thought and decided to delete first, so I click on the delete button (which is the idea for each formset. And it deletes this formset (well, or at least cleans and hides it), here .. . - b-ars07
  • So I’m talking about the same thing: when creating a form, you create a button, immediately add a handler to it, in which you delete the form that you are creating now and everything, no id needed - Grundy
  • @Grundy, thanks, I’ll try it now) - b-ars07
  • @Grundy, I understood correctly, in HTML I create input with the button type and some kind of caption. and in jQuery, when creating each form, I get this button through the DOM and hang the handler, right? or I understood something wrong .. - b-ars07

0