I have a simple multi-select dropdown list. I want to be able to add/get data to it using jQuery as I get the data from the database. I have looked at many solutions including this question. However, none of those solutions worked for me.
The multi-select list is empty as I want to add data to it using jQuery:
var values = "Test,Prof,Off";
var splitValues = values.split(',');
var multi = document.getElementById('DDLSKills');
multi.value = null; // Reset pre-selected options (just in case)
var multiLen = multi.options.length;
for (var i = 0; i < multiLen; i++) {
if (splitValues.indexOf(multi.options[i].value) >= 0) {
multi.options[i].selected = true;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<label>Skills</label>
<select id="DDLSKills" data-style="form-control" class="selectpicker form-control" data-size="5" multiple data-max-options="2">
</select>
</div>
This is the code I was using from the above quoted question but it didn't work for me.
Any help would highly be appreciated.