3

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?

1 Answer 1

5

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.

2
  • 2
    Wow. i wish I had learned this months ago, this would have saved me so much pain and suffering! Dec 3, 2015 at 1:09
  • This same fix also applies this other way around: listening to the change event. Thanks!
    – Djizeus
    Jun 2, 2016 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.