BalajiB BalajiB - 4 months ago 10
Javascript Question

Auto Select the Multiselect 2's values based on Multiselect 1 values

In my view page, I have two multiselects named multiselect1, multiselect2 . In multiselect1, Iam fetching values from database and making them selected dynamically. Based on that, the values in Multiselect1 should be selected in multiselect2 automatically

<h4>First Multiselect</h4>
<select id='multipleSelect1' multiple='multiple' style="margin-right:40px">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>

<h4> Second Multiselect </h4>

<select id='multipleSelect2' multiple='multiple'>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>


JSFiddle Link : https://jsfiddle.net/jaLygLzo/10/

Thank you

Answer

Simple enough..
Just remove the For loop..

$("#multipleSelect2").val(selected_values);

Demo

OnChange Demo