I need to set an array of data after initializing select2. So I want to make something like this:
var select = $(\'#select\').select2({}); select.data([ {id: 1
Source: https://select2.org/programmatic-control/add-select-clear-items
Add item:
var data = { id: 1, text: 'Barn owl' }; var newOption = new Option(data.text, data.id, false, false); $('#mySelect2').append(newOption).trigger('change');
Clear items:
$('#mySelect2').val(null).trigger('change');