Get value of Deselected option Multiselect
<p>I have a form that has a 2 select boxes for dependent lists.</p>

<p>I'm loading a second list based on the value selected in the first list, which is multiselect.</p>

<pre><code>&lt;select id="list1" multiple='multiple'&gt;
    &lt;option value="a"&gt;a value&lt;/option&gt;
    &lt;option value="b"&gt;b value&lt;/option&gt;
    &lt;option value="c"&gt;c value&lt;/option&gt;
&lt;/select&gt;
&lt;select id="list2"&gt;
&lt;/select&gt;
</code></pre>

<p>For each option added to list2, I'm also adding a class with value of list1 corresponding to the list2 value.</p>

<p>For loading the elements for the selected option, I use the code:</p>

<pre><code>$('#list1').on('change', function(){
    var list1vals = $('#list1').val().split(',');
    var newval = '';
    for(x in list1vals)
    {
        // Checking for selected value of list1 which doesn't have
        // corresponding values in list2.

        if($('.'+list1vals[x]).length == 0) 
        {
            newval = list1vals[x];
        }
    }
    $.ajax({
        type : "GET",
        url : "/xyz.php?action=getlist2&amp;list1="+newval,
        success : function(response)
        {
            if($.trim(response) != '')
            {
                $('#list2').append(response);
            }
        }
    });
});
</code></pre>

<p>xyz.php</p>

<pre><code>$action=$_GET['action'];
$listval1=$_GET['list1'];
switch($action)
{
    /////////////////

    case 'getlist2':
            $list2 = fetchlist2($list1val);
            foreach($list2 as $listobj)
            {
                $optionlist = '&lt;option class="'.$list1val.'" value="'.$listobj['id'].'"&gt;'.$listobj['value'].'&lt;/option&gt;';
            }
            echo $optionlist;
            break;

    ////////////////
}
</code></pre>

<p>Is there any way in which I can remove the element unselected in list1 from list2?</p>

<p>i.e when an option is unselected, I want to remove all elements in list2 having class value of the unselected element.</p>

<p>Note : The operation for fetching the list in <code>fetchlist2</code> is expensive and hence I want to handle removal of element in javascript.</p>
