Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-15T15:26:26.010Z
Answers.LastActivityDate: 2017-11-15T15:52:11.353Z
Answers.LastEditDate: 2017-11-15T15:52:11.353Z
Answers.UserDisplayName: Chattervast
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-11-15T02:39:47.420Z
Comments.Text: 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>
Comments.UserDisplayName: Chattervast
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-11-15T10:58:16.550Z
Comments.Text: Do you want to know get to know how to save your changes into the database?
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-11-15T13:10:14.133Z
Comments.Text: 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.
Comments.UserDisplayName: Chattervast
Tags: <custom-field><usermodel><drop-down>
Title: Add Checked Attribute to Custom User Fields on Frontend User Form
ThreadText: # 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') %}
  <select multiple>
      {% for opt in newsletters.settings.options %}
          <option value="{{ opt.value }}" >{{ opt.label }}</option>
      {% endfor %}
  </select>
  
  ```
  
  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 %}
      <input type="checkbox" value="{{ letter.value }}" name="{{ letter.value }}" id="{{ letter.value }}"
          {% if letter.selected == true %}checked="checked" {% endif %}/>
      <label for ="{{ letter.value }}">{{ letter.label }}</label>
  {% endfor %}
  
  ```
  
  
  ---
  
  