I have a selectbox with the multiple
attribute set. I'm also using <optgroup>
tags to separate categories within my select box. I'm looking for a way, using either javascript or jQuery, to have the various options within each group to behave with 'radio button logic' rather than 'check box logic'. For example:
<optgroup label="cat1">
<option>item 1.1</option>
<option>item 1.2</option>
</optgroup>
<optgroup label="cat2">
<option>item 2.1</option>
<option>item 2.2</option>
</optgroup>
item 1.1
in the list. item 1.1
is selected as expected. item 2.1
in the list. Now both item 1.1
and item 2.1
are selected.item 1.2
in the list. Now item 1.1
is deselected, while item 2.1
and item 1.2
are both selected. Does that make sense? Thanks in advance!
Click the first item, then press the SHIFT key and hold it. Click the last item and release the SHIFT key. To select adjacent items, you can also use the mouse. Click the left mouse button on the first item, hold the mouse button, move the cursor to the last item and then release the mouse button.
The MultiSelect has built-in support to select multiple values through checkbox, when mode property set as CheckBox . To use checkbox, inject the CheckBoxSelection module in the MultiSelect. Preview. index.js. index.html.
From Select2 docs: "Furthermore, <optgroup> elements cannot be made selectable. This is a limitation of the HTML specification and is not a limitation that Select2 can overcome."
$('#select-box-id optgroup option').click(function() {
// only affects options contained within the same optgroup
// and doesn't include this
$(this).siblings().prop('selected', false);
});
Although, truth be told, if you want "radio button logic", you might consider not using a multiple select box. Usability with multiple select boxes is a pain anyway (people have to ctrl-click to select more than one option). Consider using radio buttons, or using a single-select box for each group. Both are harder to mess up. And they work without JS, which is pretty much always a plus.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With