Skip to content

Instantly share code, notes, and snippets.

@aleger
Last active April 4, 2018 13:24
Show Gist options
  • Select an option

  • Save aleger/e5277e16047ebe31a2b56d2fb1caa420 to your computer and use it in GitHub Desktop.

Select an option

Save aleger/e5277e16047ebe31a2b56d2fb1caa420 to your computer and use it in GitHub Desktop.
Javascript helper to initialize Select2 elements and autocomplete
//select2
destroySelect2: function(element) {
$(element).empty();
$(element).select2('destroy');
},
initializeSelect2: function (element, options) {
var defaultOptions = {
placeholder: 'Select',
allowClear: true
},
newOptions = $.extend({}, defaultOptions, options);
return $(element).select2(newOptions);
},
//http://stackoverflow.com/questions/23132404/programatically-add-selected-option-into-select2
autoFillItemSelect2: function (element, item) {
$(element)
.append('<option selected="selected" value="' + item.id + '">' + item.text + '</option>')
.trigger('change');
}
@aleger
Copy link
Author

aleger commented Apr 4, 2018

USAGE

elementHelper.autoFillItemSelect2('#PolicySearchRequest_TwcCodes', { id: queryObject.twcCode, text: queryObject.twcCode });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment