<p>When you do <code>currentUser.occupation.all()</code> your <code>selected</code> variable is an objet so your conditional <code>{% if category.id == selected %}</code> will never pass as you are checking if <code>category.id</code> equals an object.</p>

<p>For this to work, first get the ids of the <code>currentUser</code> occupations:</p>

<p><code>{% set selected = currentUser.occupation.ids() %}</code></p>

<p>Which would return an array of ids (key/value pairs) then you compare doing:</p>

<p><code>{% if category.id in selected|values %}</code></p>

<p>So in full:</p>

<pre class="lang-twig prettyprint-override"><code>    &lt;select multiple name="fields[occupation][]" class="selectpicker" &gt;
        {% for category in categories %}        
        {% set selected = currentUser.occupation.ids() %}
          &lt;option value="{{ category.id }}" {% if category.id in selected|values %} selected{% endif %} &gt; {{ category.title }} &lt;/option&gt;
        {% endfor %}
    &lt;/select&gt;
</code></pre>

CC BY-SA 4.0
2019-05-15T13:05:50.977Z
2019-05-15T13:05:50.977Z
Oli
<p>I have a users form, which they can select their occupations. They can choose multiple values from a multi-select input.</p>

<p>Occupations are categories that are related to users profile. </p>

<p>Here in this code, i can get the values and submit witout any problem,but i cannot show as selected when user returns to form. They should see thier previous selections.</p>

<pre><code>{% set categories = craft.categories.group('occupation').all() %}  
&lt;input type="hidden" name="fields[occupation][]" value=""&gt;
&lt;select multiple name="fields[occupation][]" class="selectpicker" &gt;

{% for category in categories %}        
  {% set selected = currentUser.occupation.all()  %}    
  &lt;option value="{{ category.id }}" {% if category.id == selected  %} selected{% endif %} &gt; {{ category.title }} &lt;/option&gt;
{% endfor %}
</code></pre>

<p></p>

<p>How can i access to selected arrays id value?</p>

<craft3><categories><form><user-profile>
Trying to show selected categories in a multi select form input
# Trying to show selected categories in a multi select form input

(Asked by: username_0 on 2019-05-10)

I have a users form, which they can select their occupations. They can choose multiple values from a multi-select input.
Occupations are categories that are related to users profile. 
Here in this code, i can get the values and submit witout any problem,but i cannot show as selected when user returns to form. They should see thier previous selections.
{% set categories = craft.categories.group('occupation').all() %}  



{% for category in categories %}        
  {% set selected = currentUser.occupation.all()  %}    
   {{ category.title }} 
{% endfor %}


How can i access to selected arrays id value?

**Tags:** craft3, categories, form, user-profile


---

## 1 answers

### Answer by username_1 on 2019-05-15. Score: 2 | Accepted answer

When you do currentUser.occupation.all() your selected variable is an objet so your conditional {% if category.id == selected %} will never pass as you are checking if category.id equals an object.
For this to work, first get the ids of the currentUser occupations:
{% set selected = currentUser.occupation.ids() %}
Which would return an array of ids (key/value pairs) then you compare doing:
{% if category.id in selected|values %}
So in full:
    
        {% for category in categories %}        
        {% set selected = currentUser.occupation.ids() %}
           {{ category.title }} 
        {% endfor %}


---

