1

I have a listbox on a Django form

forms.MultipleChoiceField(widget=forms.widgets.SelectMultiple())

How can I enable multiple choice with a simple click to select/unselect , without having to press the ctrl key ?

Nico44044
  • 125
  • 8

1 Answers1

1

It might be better to just write this as a list of checkboxes, so with a CheckboxSelectMultiple [Django-doc]:

my_field = forms.MultipleChoiceField(widget=forms.widgets.CheckboxSelectMultiple)

You can wrap it into a scrollable, but by working with checkboxes, it is clear what is selected and what not, and one can easily toggle a single element.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • It may be a good alternative thanks. but if possible, I would really rather stick with the Listbox style – Nico44044 May 19 '23 at 13:06
  • @Nico44044: well Django does not control that, that is the browser that determines how a ` – Willem Van Onsem May 19 '23 at 13:10
  • Ok I got it. I will try to go with your solution and adapt the rest of my code. Thanks – Nico44044 May 19 '23 at 13:11
  • Just si I know, do you have a hint on how to fix this behavior in JS ? – Nico44044 May 19 '23 at 13:16
  • @Nico44044: well most javascript libraries will create something themselves with a `
    ` that thus *looks* like a select, but is not, like a select2: https://select2.org/getting-started/basic-usage#multi-select-boxes-pillbox
    – Willem Van Onsem May 19 '23 at 13:29