Question

<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.

Was it helpful?

Solution

Set the selected property on the options, rather than setting the value of the select:

$("#program option[value=" + e + "]").prop('selected', true);

Here's a fiddle

OTHER TIPS

billyonecan's answer is correct. If you want more advanced multiselect, then see here. Demos here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top