Reputation: 3896
<select id="program" multiple="multiple">
<option value="movie">Movie</option>
<option value="series">TV Series</option>
<option value="episode">Episode</option>
</select>
In the above select, I want to dynamically select option movie and series.
var options = [movie,series]
options.forEach(function(e){
$("#program select").attr('value',e);
});
Above code selects only series option.
Upvotes: 1
Views: 3253
Reputation: 20250
Set the selected
property on the options, rather than setting the value
of the select:
$("#program option[value=" + e + "]").prop('selected', true);
Upvotes: 3
Reputation: 17264
billyonecan's answer is correct. If you want more advanced multiselect, then see here. Demos here.
Upvotes: 0