I managed to select all of the options from a multiselect, however, I am trying to select Test2 and Test3 only. I have an array $arr which contains some values I want to select. for example $arr = ('Test2', 'Test3')
<select id="test" multiple="multiple">
<option>Test</option>
<option>Test2</option>
<option>Test3</option>
<option>Test4</option>
</select>
Selecting all:
$('#test option').attr('selected', 'selected');
$('#test option:nth-child(2)').attr('selected', 'selected'); $('#test option:nth-child(3)').attr('selected', 'selected');var multi = $('#test').val(), two = multi[1], three = multi[2]; console.log(two); console.log(three);