Answers.Body: <p>There is a <a href="https://twig.symfony.com/doc/2.x/templates.html#containment-operator" rel="nofollow noreferrer">in</a> operator to check if a variable is in a string or an array. Your <code>entries</code> field actually contains an object of type <code>ElementQuery</code> instead of an array of elements.</p>
  
  <p>All you need to do is to check if the current entry is in the list of selected entries </p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set selectedEntries = entry.planItems.ids() %}
  
  {% set entries = craft.entries.section('exercises') %}
  {% for entry in entries %}
      &lt;li&gt;
          &lt;input 
              {% if entry.id in selectedEntries %}checked{%endif%}
              type="checkbox" 
              name="fields[planItems][]" 
              value="{{ entry.id }}"&gt;{{ entry.title }}
      &lt;/li&gt;
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-25T18:31:58.277Z
Answers.Text: You legend Robin, thank you for that information. I never realized that in operator before and I totally had no idea how to check if the current entry is in the list. Quick typo on the last endif, just had to change that to endfor. Thanks so much again for your help!
Answers.UserDisplayName: Kieron Keenan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-25T18:09:43.097Z
Answers.LastActivityDate: 2018-08-25T18:21:34.240Z
Answers.LastEditDate: 2018-08-25T18:21:34.240Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I currently have front-end code working for a user to create an entry, add a title (text input) and also relate entries via the entries field type and checkboxes. All on a front-end application.</p>
  
  <p>Creating the entry is fine, but I'm having an issue with the editing of the entry.</p>
  
  <p>I'm unable/unsure how to "check" what the previously selected relate entries where. Code for showing entry items...</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set entries = craft.entries.section('exercises') %}
  {% for entry in entries %}
  &lt;li&gt;
      &lt;input type="checkbox" name="fields[planItems][]" 
             id="fields[planItems]" value="{{ entry.id }}"&gt;{{ entry.title }}
  &lt;/li&gt;
  </code></pre>
  
  <p>Other fields are fine such as the title field, all you have to add is <code>{%- if entry is defined %} value="{{ entry.title }}"{% endif -%}&gt;</code>.</p>
  
  <p>Any ideas on how to get that value so I can add <code>checked</code> to the previously selected entries?</p>
  
  <p>Thanks!</p>
  
Tags: <entries><relatedto><frontend-form>
Title: Edit entry, on the front end, with the entries field
ThreadText: # Edit entry, on the front end, with the entries field
  
  (Asked by: username_0 on 2018-08-25)
  
  I currently have front-end code working for a user to create an entry, add a title (text input) and also relate entries via the entries field type and checkboxes. All on a front-end application.
  
  Creating the entry is fine, but I'm having an issue with the editing of the entry.
  
  I'm unable/unsure how to "check" what the previously selected relate entries where. Code for showing entry items...
  
  ```twig
  {% set entries = craft.entries.section('exercises') %}
  {% for entry in entries %}
  <li>
      <input type="checkbox" name="fields[planItems][]" 
             id="fields[planItems]" value="{{ entry.id }}">{{ entry.title }}
  </li>
  
  ```
  
  Other fields are fine such as the title field, all you have to add is `{%- if entry is defined %} value="{{ entry.title }}"{% endif -%}>`.
  
  Any ideas on how to get that value so I can add `checked` to the previously selected entries?
  
  Thanks!
  
  **Tags:** entries, relatedto, frontend-form
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-08-25. Score: 4 | Accepted answer
  
  There is a [in](https://twig.symfony.com/doc/2.x/templates.html#containment-operator) operator to check if a variable is in a string or an array. Your `entries` field actually contains an object of type `ElementQuery` instead of an array of elements.
  
  All you need to do is to check if the current entry is in the list of selected entries
  
  ```twig
  {% set selectedEntries = entry.planItems.ids() %}
  
  {% set entries = craft.entries.section('exercises') %}
  {% for entry in entries %}
      <li>
          <input 
              {% if entry.id in selectedEntries %}checked{%endif%}
              type="checkbox" 
              name="fields[planItems][]" 
              value="{{ entry.id }}">{{ entry.title }}
      </li>
  {% endif %}
  
  ```
  
  > **Comments:**
  > **username_0**: You legend username_1, thank you for that information. I never realized that in operator before and I totally had no idea how to check if the current entry is in the list. Quick typo on the last endif, just had to change that to endfor. Thanks so much again for your help!  
  
  ---
  
  