<p>Solved!</p>

<pre><code>{% for letter in currentUser.newsletters.options %}
    &lt;input type="checkbox" value="{{ letter.value }}" name="{{ letter.value }}" id="{{ letter.value }}"
        {% if letter.selected == true %}checked="checked" {% endif %}/&gt;
    &lt;label for ="{{ letter.value }}"&gt;{{ letter.label }}&lt;/label&gt;
{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-11-15T15:26:26.010Z
2017-11-15T15:52:11.353Z
2017-11-15T15:52:11.353Z
Chattervast
<p>I have a multi-select for users to sign up for newsletters.
When they edit their profile they need to be able to see which newsletters they are already signed up for. I have the code to output the entire options list or just the ones that they have selected but I haven't figured a way to add the selected attribute to the ones the user has selected.</p>

<pre><code>{% set newsletters = craft.fields.getFieldByHandle('newsletters') %}
&lt;select multiple&gt;
    {% for opt in newsletters.settings.options %}
        &lt;option value="{{ opt.value }}" &gt;{{ opt.label }}&lt;/option&gt;
    {% endfor %}
&lt;/select&gt;
</code></pre>

<p>The "for opt in newsletters.settings.options" can be changed to "for opt in newsletters.settings" to show just the ones the user has already selected.</p>

CC BY-SA 3.0
2017-11-15T02:39:47.420Z
Guessing this should work but form reloads page and doesn't save
{% set newsletters = craft.fields.getFieldByHandle('newsletters') %}
<select name="fields[newsletters]" id="newsletters" multiple>
{% for opt in newsletters.settings.options %}
{% set created = false %}
{% for opti in newsletters.settings %}
{% if opti.value == opt.value %}
<option value="{{ opt.value }}" selected="selected">{{ opt.label }}</option>
{% set created = true %}
{% endif %}
{% endfor %}
{% if created == false %}
<option value="{{ opt.value }}">{{ opt.label }}</option>
{% endif %}
{% endfor %}
</select>
Chattervast
CC BY-SA 3.0
2017-11-15T10:58:16.550Z
Do you want to know get to know how to save your changes into the database?
Robin Schambach
CC BY-SA 3.0
2017-11-15T13:10:14.133Z
Yes, I need it to save the changes and also have a way to add selected or some attribute if the user had previously selected that option.
Chattervast
<custom-field><usermodel><drop-down>
Add Checked Attribute to Custom User Fields on Frontend User Form
# Add Checked Attribute to Custom User Fields on Frontend User Form

(Asked by: username_0 on 2017-11-15)

I have a multi-select for users to sign up for newsletters.
When they edit their profile they need to be able to see which newsletters they are already signed up for. I have the code to output the entire options list or just the ones that they have selected but I haven't figured a way to add the selected attribute to the ones the user has selected.
{% set newsletters = craft.fields.getFieldByHandle('newsletters') %}

    {% for opt in newsletters.settings.options %}
        {{ opt.label }}
    {% endfor %}


The "for opt in newsletters.settings.options" can be changed to "for opt in newsletters.settings" to show just the ones the user has already selected.

**Tags:** custom-field, usermodel, drop-down
> **Comments:**
> **username_0**: Guessing this should work but form reloads page and doesn't save {% set newsletters = craft.fields.getFieldByHandle('newsletters') %} <select name="fields[newsletters]" id="newsletters" multiple> {% for opt in newsletters.settings.options %} {% set created = false %} {% for opti in newsletters.settings %} {% if opti.value == opt.value %} <option value="{{ opt.value }}" selected="selected">{{ opt.label }}</option> {% set created = true %} {% endif %} {% endfor %} {% if created == false %} <option value="{{ opt.value }}">{{ opt.label }}</option> {% endif %} {% endfor %} </select>  
> **username_1**: Do you want to know get to know how to save your changes into the database?  
> **username_0**: Yes, I need it to save the changes and also have a way to add selected or some attribute if the user had previously selected that option.  

---

## 1 answers

### Answer by username_0 on 2017-11-15. Score: 3 | Accepted answer

Solved!
{% for letter in currentUser.newsletters.options %}
    
    {{ letter.label }}
{% endfor %}


---

