Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created August 23, 2018 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/3444d2be3556fc0c67036d5e3b2a6390 to your computer and use it in GitHub Desktop.
Save tommcfarlin/3444d2be3556fc0c67036d5e3b2a6390 to your computer and use it in GitHub Desktop.
[WordPress] Triggering Angular Events with jQuery
$('select[ng-model="schedule.payment_method"]').each(function() {
// For the purposes of this example, we're going to select the first option.
$(this)
.children('option:eq(1)')
.attr('selected', 'selected');
// Make sure the select element reflects the change (as setting the option doesn't always do this properly).
$(this).val($(this).children('option:selected').attr('value'));
// Now use the Angular API's triggerHandler function to call the change.
angular.element($(this)).triggerHandler('change');
});
<select class="form-control ng-pristine ng-untouched ng-valid ng-empty" ng-model="item.payment_method" ng-change="updatePaymentMethod(item)" ng-options="method.display_name for method in paymentMethods track by method.id">
<!-- more to come -->
</select>
<select class="form-control ng-pristine ng-untouched ng-valid ng-empty" ng-model="item.payment_method" ng-change="updatePaymentMethod(item)" ng-options="method.display_name for method in paymentMethods track by method.id">
<option value="?"></option>
<option value="1042" selected="selected">Card 1001 (01/18)</option>
</select>
$('select['ng-model="item.payment_method"]').trigger('change');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment