I have a standard select multiple
HTML Input field, for example:
<select multiple="multiple" size="5" id="mysel" name="countries">
<option value="2">Afghanistan</option>
<option value="4">Aland</option>
</select>
As this is a multi-select, to select more than one value you have to hold the CTRL key and select any further elements. However what I want to achieve is, that:
- Clicking an UNSELECTED option SELECTs it
- Clicking a SELECTED option UNSELECTS it.
The idea is to avoid having to press the CTRL key and change the usage semantics of this input field. Elements should only be selectable and un-selectable by clicking (ie. toggling of the select status).
I have not yet been able to implement this. The pseudo-code should look something like this.
- Catch a Click event.
- Check if the element that was clicked was unselected, then select it
- Or if the element that was clicked was selected, then unselect it.
How should I implement this?
You may use the following snippet to achieve your desired effect
In older versions of jQuery, where
prop()
was not available: