DarkLeafyGreen
DarkLeafyGreen

Reputation: 70416

Selected element cannot be deselected when select attribute is set

Hi have a dropdown list that is filled with data from database.

<select name="form[entities][]" multiple="true">
        {% for entity in entities %}
            {% if entity.selected %}

            <option value="{{ entity.id }}" selected="selected">
                 {{ entity.name }}
            </option>

            {% else %}

            <option value="{{ entity.id }}">{{ entity.name }}</option>

            {% endif %}
        {% endfor %}
</select>

If an entity was previously slected it will be rendered as selected.

I can switch selections, that means I can select another entity and the previous one will be deselected. But e.g. if there is only one item and it is selected it cannot be deselected it simply stays selected.

So my question is, is this normal behaviour? The field is not required. Is there a way to handle deselection just using html or do I have to introduce another button and handle deselection with javascript?

Upvotes: 0

Views: 174

Answers (1)

Nomand
Nomand

Reputation: 36

There are a couple of possible answers

  • show to users to use CTRL to deselect last selected option
  • add a blank option
  • handle deselection with jquery

Upvotes: 2

Related Questions