CheckboxSelectMultiple check first item

I am using a form field of this type, I attach photo below, which content is generated at runtime with JQuery based on a choice of another field.
The bug occurs when I click anywhere in the choices, but not on the other check boxes, in that case everything is fine.

Form Field:
variables_new = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple(), queryset=struct_detail_repository.get(), label='Variables', required=True)

JQuery dinamicaly choices:

$.each(data, function (index, value) {
     $('#id_variables_new').append('<li><label for="id_variables_new_"' + index + '"> <input type="checkbox" name="variables_new" value="' + value['value'] + '" id="id_variables_new_"' + index + '"> ' + value['label'] + ' </label> </li >');


This isn’t Django related, but I think this line is your issue.

You are ending the for and id strings too early with double quotes.

'<li><label for="id_variables_new_"' + index + '">...

will turn in to

<li><label for="id_variables_new_"0">...

Note the extra " before the index.

You should have

'<li><label for="id_variables_new_' + index + '">...

…and the same for the inout.

1 Like