Body: <p>I have a scenario where a user selects a row of data to edit and I want an EntityRef Select2 box to be populated with a piece of information from that row.</p>
  
  <p>The EntityRef select2 widget works for selecting and submitting data, but I can't seem to select a value programmatically. </p>
  
  <p>I have tried a number of solutions with varying levels of non-success. Sometimes, the element doesn't change at all. Sometimes it says "undefined". </p>
  
  <p>The below selects the first element in the list, but then enters some sort of infinite loop, where according to the console a requests are constantly being sent and I'm unable to open the Select2 dropdown because of it. </p>
  
  <pre><code>$('#s2id_dc_group_id').select2('val', dcName); //infinite loop.
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>Here is the HTML for the input field that we will be using:</p>
  
  <pre><code>&lt;input id="dc_group_id" class="form-control" name="dc_group_field" placeholder="{ts}- select DC -{/ts}" /&gt;
  </code></pre>
  
  <p>And here is where we setup the EntityRef (we call this function inside a document ready block):</p>
  
  <pre><code>function SetupEntityRef() {
      CRM.api3('Group', 'get', {
          "sequential": 1,
          "title": "Discipleship Community",
          "return": ["children"],
      }).done(function (result) {
          console.log(result);
          var dc_groups = result['values'][0]['children'].split(',');
          console.log('DC Groups: ' , dc_groups);
          $('[name=dc_group_field]').crmEntityRef({
              entity: 'group',
              api: {
                  params: {
                      id: {"IN":dc_groups},  // dc ids
                  }},
              create: false,
              select: {minimumInputLength: 0} // show results without having to search
          });
  
      });
  }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-08T19:42:50.970Z
Comments.Text: Are you using CiviCRM?
Comments.UserDisplayName: Pradeep Nayak
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-08T19:45:49.647Z
Comments.Text: @PradeepNayak Yes, CiviCRM and Drupal.
Comments.UserDisplayName: Alec Macleod
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-08T19:47:54.137Z
Comments.Text: So can you please mention the field name you trying to select?
Comments.UserDisplayName: Pradeep Nayak
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-08T19:52:00.647Z
Comments.Text: The name of the field (before select2 is applied to it) is "dc_group_field".
Comments.UserDisplayName: Alec Macleod
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-08T20:01:20.967Z
Comments.Text: So did you try $('#dc_group_field').val(dcName);
Comments.UserDisplayName: Pradeep Nayak
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-08T20:11:06.970Z
Comments.Text: That doesn't do anything, but I think that is because that isn't the proper way to access an element using "name". I have tried this using the input field ID "dc_group_id", and it still results in an infinite loop.
Comments.UserDisplayName: Alec Macleod
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-09T09:54:05.773Z
Comments.Text: Could you update the question with the piece of raw HTML and your JavaScript code? That would be helpful to understand a bit more and get good help form the community.
Comments.UserDisplayName: Andrei
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-10T16:30:17.410Z
Comments.Text: Updated with more code. I didn't include the select2 specific generated code, but I can do that if necessary.
Comments.UserDisplayName: Alec Macleod
Tags: <javascript><select2><entityref>
Title: How do you select a value of an EntityRef Select2 using jQuery?
ThreadText: # How do you select a value of an EntityRef Select2 using jQuery?
  
  (Asked by: username_0 on 2018-10-08)
  
  I have a scenario where a user selects a row of data to edit and I want an EntityRef Select2 box to be populated with a piece of information from that row.
  
  The EntityRef select2 widget works for selecting and submitting data, but I can't seem to select a value programmatically.
  
  I have tried a number of solutions with varying levels of non-success. Sometimes, the element doesn't change at all. Sometimes it says "undefined".
  
  The below selects the first element in the list, but then enters some sort of infinite loop, where according to the console a requests are constantly being sent and I'm unable to open the Select2 dropdown because of it.
  
  ```
  $('#s2id_dc_group_id').select2('val', dcName); //infinite loop.
  
  ```
  
  UPDATE:
  
  Here is the HTML for the input field that we will be using:
  
  ```
  <input id="dc_group_id" class="form-control" name="dc_group_field" placeholder="{ts}- select DC -{/ts}" />
  
  ```
  
  And here is where we setup the EntityRef (we call this function inside a document ready block):
  
  ```
  function SetupEntityRef() {
      CRM.api3('Group', 'get', {
          "sequential": 1,
          "title": "Discipleship Community",
          "return": ["children"],
      }).done(function (result) {
          console.log(result);
          var dc_groups = result['values'][0]['children'].split(',');
          console.log('DC Groups: ' , dc_groups);
          $('[name=dc_group_field]').crmEntityRef({
              entity: 'group',
              api: {
                  params: {
                      id: {"IN":dc_groups},  // dc ids
                  }},
              create: false,
              select: {minimumInputLength: 0} // show results without having to search
          });
  
      });
  }
  
  ```
  
  **Tags:** javascript, select2, entityref
  
  > **Comments:**
  > **username_1**: Are you using CiviCRM?  
  > **username_0**: @username_1 Yes, CiviCRM and Drupal.  
  > **username_1**: So can you please mention the field name you trying to select?  
  > **username_0**: The name of the field (before select2 is applied to it) is "dc_group_field".  
  > **username_1**: So did you try $('#dc_group_field').val(dcName);  
  > **username_0**: That doesn't do anything, but I think that is because that isn't the proper way to access an element using "name". I have tried this using the input field ID "dc_group_id", and it still results in an infinite loop.  
  > **username_2**: Could you update the question with the piece of raw HTML and your JavaScript code? That would be helpful to understand a bit more and get good help form the community.  
  > **username_0**: Updated with more code. I didn't include the select2 specific generated code, but I can do that if necessary.  
  
  ---
  
  ## 0 answers
  
  