Answers.Body: <p>According to <a href="https://civicrm.stackexchange.com/questions/18627/how-to-select-multiple-values-in-select2-control-by-jquery">this related question</a>, it should be possible to do this via:</p>
  
  <pre><code>$('#id-of-select2').select2('val', 'value-to-select');
  </code></pre>
  
  <p>or, if you want to select multiple items in a multiselect:</p>
  
  <pre><code>$('#id-of-select2').select2('val', ['value-to-select', 'another-value']);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-07T03:10:58.087Z
Answers.LastActivityDate: 2017-08-07T03:10:58.087Z
Answers.UserDisplayName: Chris Burgess
Body: <p>The CiviCRM <a href="https://wiki.civicrm.org/confluence/display/CRMDOC/EntityRef+Fields" rel="nofollow noreferrer">EntityRef</a> form element uses a <a href="https://select2.github.io/" rel="nofollow noreferrer">Select2</a> select box to make lots of CiviCRM forms more awesome. A user can, for example, search for contacts to choose one. Great.</p>
  
  <p>I am building an extension where I would like to take an EntityRef element from a core form and select a use javascript to select a specific entity (or multiple entities in the case of a multi-select). Is there a javascript function I can call in order to trigger such a selection (by passing in the ID of the entity, for example)?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-08-07T03:09:26.960Z
Comments.Text: Have you seen https://civicrm.stackexchange.com/questions/18627/how-to-select-multiple-values-in-select2-control-by-jquery ?
Comments.UserDisplayName: Chris Burgess
Tags: <developer><forms><javascript><select2><entityref>
Title: How can I trigger a selection for a Select2 EntityRef element via javascript?
ThreadText: # How can I trigger a selection for a Select2 EntityRef element via javascript?
  
  (Asked by: username_0 on 2017-08-06)
  
  The CiviCRM [EntityRef](https://wiki.civicrm.org/confluence/display/CRMDOC/EntityRef+Fields) form element uses a [Select2](https://select2.github.io/) select box to make lots of CiviCRM forms more awesome. A user can, for example, search for contacts to choose one. Great.
  
  I am building an extension where I would like to take an EntityRef element from a core form and select a use javascript to select a specific entity (or multiple entities in the case of a multi-select). Is there a javascript function I can call in order to trigger such a selection (by passing in the ID of the entity, for example)?
  
  **Tags:** developer, forms, javascript, select2, entityref
  
  > **Comments:**
  > **username_1**: Have you seen https://civicrm.stackexchange.com/questions/18627/how-to-select-multiple-values-in-select2-control-by-jquery ?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-08-07. Score: 1
  
  According to [this related question](https://civicrm.stackexchange.com/questions/18627/how-to-select-multiple-values-in-select2-control-by-jquery), it should be possible to do this via:
  
  ```
  $('#id-of-select2').select2('val', 'value-to-select');
  
  ```
  
  or, if you want to select multiple items in a multiselect:
  
  ```
  $('#id-of-select2').select2('val', ['value-to-select', 'another-value']);
  
  ```
  
  
  ---
  
  