Q:

jQuery UI MultiSelect Widget -Select only one from optgroup options in multiselect drop down

I have drop down like this
<select multiple="multiple">
     <optgroup label='name1'>
     <option value="1">First</option>
     <option value="2">Second</option>
     <option value="3">Third</option>
</optgroup>

<optgroup label='name2'>
    <option value="1">First</option>
    <option value="2">Second</option>
    <option value="3">Third</option>
 </optgroup> 

Iam Using jQuery UI MultiSelect Widget plugin for multi drop down,But i should select only one option from each optgroup options.
Eg. If option "First" is selected for 'name1' then "second","third" should not be selectable for 'name1' optgroup options.

A:

Try this
     var memoryOne;
        var memoryTwo;
        $("option").mouseover(function () {
            var selectedOne = $("optgroup:nth-of-type(1)").children("option:selected").index();
            var selectedTwo = $("optgroup:nth-of-type(2)").children("option:selected").index();
            memoryOne = selectedOne;
            memoryTwo = selectedTwo;
        }).click(function () {
            var theSelectedIndex = $(this).index();
            var theParentIndex = $(this).parent().index();
            setTimeout(function () {
                clickEvent(theSelectedIndex, theParentIndex, memoryOne, memoryTwo);
            }, 1);
        });

        function clickEvent(passedIndex, parentIndex, memoryOne, memoryTwo) {
            var selectionOne = memoryOne;
            var selectionTwo = memoryTwo;
            var theParent = $("optgroup:eq(" + parentIndex + ")");
            var theOption = $("optgroup:eq(" + parentIndex + ") option:eq(" + passedIndex + ")");
            var theGrandParent = $("select");
            theParent.find("option:not(option:eq(" + passedIndex + "))").prop("selected", false);

        if (parentIndex == 0) {
            $("optgroup:not(optgroup:eq(" + parentIndex + "))").children("option:eq(" + selectionTwo + ")").prop("selected", true);
        } else if (parentIndex == 1) {
            $("optgroup:not(optgroup:eq(" + parentIndex + "))").children("option:eq(" + selectionOne + ")").prop("selected", true);
        }

}

DEMO

