Answers.Body: <p>Figured this out. I needed to wrap my jQuery code in:</p>
  
  <pre><code>CRM.$(function($) {
  
  });
  </code></pre>
  
  <p>The change trigger - and other select2 events - started working once I did this.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-03T01:09:40.860Z
Answers.Text: Wow. i wish I had learned this months ago, this would have saved me so much pain and suffering!
Answers.UserDisplayName: Do Not Track Me
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-02T15:56:37.257Z
Answers.Text: This same fix also applies this other way around: listening to the change event. Thanks!
Answers.UserDisplayName: Djizeus
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-13T13:39:33.697Z
Answers.LastActivityDate: 2015-08-13T13:39:33.697Z
Answers.UserDisplayName: Andrew West
Body: <p>I'm trying to select the value of a select2 widget using js. I can change the value ok:</p>
  
  <pre><code>jQuery('#excludeGroups').val("846").trigger("change");
  </code></pre>
  
  <p>The problem I'm having is that this doesn't update the display. It <em>does</em> work in that when you submit the form it's the correct value - so it's working behind the scenes. But it doesn't actually change the display - the new value doesn't <em>appear</em> in the widget. The documentation suggests trigger("change") should do it. I'm not getting any console errors. Is there a trick?</p>
  
Tags: <select2>
Title: How do you visually update a select2 widget?
ThreadText: # How do you visually update a select2 widget?
  
  (Asked by: username_0 on 2015-08-12)
  
  I'm trying to select the value of a select2 widget using js. I can change the value ok:
  
  ```
  jQuery('#excludeGroups').val("846").trigger("change");
  
  ```
  
  The problem I'm having is that this doesn't update the display. It *does* work in that when you submit the form it's the correct value - so it's working behind the scenes. But it doesn't actually change the display - the new value doesn't *appear* in the widget. The documentation suggests trigger("change") should do it. I'm not getting any console errors. Is there a trick?
  
  **Tags:** select2
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2015-08-13. Score: 5 | Accepted answer
  
  Figured this out. I needed to wrap my jQuery code in:
  
  ```
  CRM.$(function($) {
  
  });
  
  ```
  
  The change trigger - and other select2 events - started working once I did this.
  
  > **Comments:**
  > **username_1**: Wow. i wish I had learned this months ago, this would have saved me so much pain and suffering!  
  > **username_2**: This same fix also applies this other way around: listening to the change event. Thanks!  
  
  ---
  
  