<p>This would work for both with or without <code>optgroup</code> (replace <code>aDropdown</code> with your fieldname):</p>

<pre class="lang-twig prettyprint-override"><code>{% set field = craft.app.fields.getFieldByHandle('aDropdown') %}

{# If we have optgroups in this field, set a counter to 0 else return false #}

{% set optgroupCount = field.optgroups ? 0 : false %}
&lt;select name="fields[aDropdown]"&gt;
    {% for option in field.options %}

        {% if option.optgroup is not defined %}
            {# This is a regular option #}
            {% set selected = entry is defined ? entry.aDropdown.value == option.value : option.default %}
            &lt;option value="{{ option.value }}" {% if selected %}selected{% endif %}&gt;
                {{ option.label }}
            &lt;/option&gt;
        {% else %}
            {# This is an optgroup #}
            {# Check if we should close a group before opening a new one.#}
            {{ optgroupCount &gt; 0 ? '&lt;/optgroup&gt;' }}
            &lt;optgroup label="{{ option.optgroup }}"&gt;
            {% set optgroupCount = optgroupCount + 1 %}
        {% endif %}

    {% endfor %}
    {# If we had optgroups better close the last one #}
    {{ optgroupCount ? '&lt;/optgroup&gt;' }}
&lt;/select&gt;
</code></pre>

CC BY-SA 4.0
2019-09-01T16:19:01.297Z
2019-09-01T16:19:01.297Z
Oli
<p>FYI, small change to get this working in Craft 4 via Craft support/Oli. Change the 3rd line in the above to:</p>
<pre><code>{% set optgroupCount = 'optgroup' in 'field.options.optgroup' ? 0 : false %}
</code></pre>

CC BY-SA 4.0
2023-11-21T14:22:39.627Z
2023-11-21T14:22:39.627Z
Ian Ebden
<p>The display of a <a href="https://docs.craftcms.com/v3/dropdown-fields.html#templating" rel="nofollow noreferrer">dropdown</a> without optgroups is defined:</p>

<pre><code>{% set field = craft.app.fields.getFieldByHandle('&lt;FieldHandle&gt;') %}

&lt;select name="fields[&lt;FieldHandle&gt;]"&gt;
    {% for option in field.options %}

        {% set selected = entry is defined
            ? entry.&lt;FieldHandle&gt;.value == option.value
            : option.default %}

        &lt;option value="{{ option.value }}"
                {% if selected %}selected{% endif %}&gt;
            {{ option.label }}
        &lt;/option&gt;
    {% endfor %}
&lt;/select&gt;
</code></pre>

<p>The following error occurs:</p>

<pre><code>Key "default" for array with keys "optgroup" does not exist.
</code></pre>

<craft3><plugin-craftcommerce><plugin-craftcommerce2>
How-to display a dropdown field with optgroups?
# How-to display a dropdown field with optgroups?

(Asked by: username_0 on 2019-08-31)

The display of a dropdown without optgroups is defined:
{% set field = craft.app.fields.getFieldByHandle('') %}


    {% for option in field.options %}

        {% set selected = entry is defined
            ? entry..value == option.value
            : option.default %}

        
            {{ option.label }}
        
    {% endfor %}


The following error occurs:
Key "default" for array with keys "optgroup" does not exist.

**Tags:** craft3, plugin-craftcommerce, plugin-craftcommerce2

---

## 2 answers

### Answer by username_1 on 2019-09-01. Score: 3 | Accepted answer

This would work for both with or without optgroup (replace aDropdown with your fieldname):
{% set field = craft.app.fields.getFieldByHandle('aDropdown') %}

{# If we have optgroups in this field, set a counter to 0 else return false #}

{% set optgroupCount = field.optgroups ? 0 : false %}

    {% for option in field.options %}

        {% if option.optgroup is not defined %}
            {# This is a regular option #}
            {% set selected = entry is defined ? entry.aDropdown.value == option.value : option.default %}
            
                {{ option.label }}
            
        {% else %}
            {# This is an optgroup #}
            {# Check if we should close a group before opening a new one.#}
            {{ optgroupCount > 0 ? '' }}
            
            {% set optgroupCount = optgroupCount + 1 %}
        {% endif %}

    {% endfor %}
    {# If we had optgroups better close the last one #}
    {{ optgroupCount ? '' }}


---

### Answer by username_2 on 2023-11-21. Score: 1

FYI, small change to get this working in Craft 4 via Craft support/Oli. Change the 3rd line in the above to:
{% set optgroupCount = 'optgroup' in 'field.options.optgroup' ? 0 : false %}


---

