From e04675a234716c3743f7ec02db2930f4a050f842 Mon Sep 17 00:00:00 2001 From: alexweissman Date: Wed, 11 Oct 2017 22:31:19 -0400 Subject: [PATCH] selecting multiple items programmatically (#5068) --- .../01.add-select-clear-items/docs.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pages/12.programmatic-control/01.add-select-clear-items/docs.md b/pages/12.programmatic-control/01.add-select-clear-items/docs.md index ed603218..b3f3443c 100644 --- a/pages/12.programmatic-control/01.add-select-clear-items/docs.md +++ b/pages/12.programmatic-control/01.add-select-clear-items/docs.md @@ -38,12 +38,19 @@ if ($('#mySelect2').find("option[value='" + data.id + "']").length) { } ``` -## Selecting an option +## Selecting options To programmatically select an option/item for a Select2 control, use the jQuery `.val()` method: ``` -$('#mySelect2').val('US'); // Select the option with a value of 'US' +$('#mySelect2').val('1'); // Select the option with a value of '1' +$('#mySelect2').trigger('change'); // Notify any JS components that the value changed +``` + +You can also pass an array to `val` make multiple selections: + +``` +$('#mySelect2').val(['1', '2']); $('#mySelect2').trigger('change'); // Notify any JS components that the value changed ```