<div class="form-group">
<select class="form-control js-example-basic-multiple"
name="regular[]" id="regular" multiple="multiple" required>
<option value="History">History</option>
<option value="Political Science">Political Science
</option>
<option value="Economics">Economics</option>
<option value="Education">Education</option>
<option value="Philosophy">Philosophy</option>
<option value="Elective Assamese">Elective Assamese
</option>
<option value="Elective Hindi">Elective Hindi</option>
<option value="Arabic">Arabic</option>
<option value="Mathematics">Mathematics</option>
<option value="Linguistics">Linguistics</option>
</select>
</div>
This is my select2 input.
$(document).ready(function () {
$('#regular').select2({
placeholder: 'Select any two from the options',
maximumSelectionLength: 2,
});
});
I've loaded the select2.js here. and I am fetching data from ajax call.
options = data.regular.split(",");
this option has 3 values, I want to select the first two from the array.
Hence My code is..
option = [options[0],options[1]];
$("#regular").val(option).trigger('change');
Now the options are showing in the list. But if I click and select from the input, everything resets and I have to select again. And if without changing anything I want to submit, the field shows required.