I have problem with iteration in jquery that i dont understand. I grab all select2 elements having given name and try to append new option:
var newOption = new Option(model.nazwa, model.id, false, false);
$('select[name="grupa_cechy_id"]').each(function () {
$(this).append(newOption).trigger('change');
//check the loop works
let idx = $(this).attr('id');
console.log(idx);
});
The loop works but only last select2 element from the loop gets new option. How to fix that?