I have a span Id with some values that correspond to values in multiple select box items. I want to take the values from span Id and select the corresponding items in list.
<span id="test">
1,4
</span>
<select id='multipleSelect' multiple='multiple'>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
I can read the string into array but not sure how to use array to select items in multiple Select box
var test = $("#test").text();
var testArray = test.split(',');
I guess if the values are known then it is easy to do something like this
$('#multipleSelect').val(['1', '2']);
Here is JS fiddle to illustrate the idea so far https://jsfiddle.net/deu8ftpb/1/
$('#multipleSelect').val($("#test").text().trim().split(','));jsfiddle.net/8621wujp