Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When select one option from "multiple select", remove it from other selects (and reverse)

I have 3 multiple selects like:

<select multiple>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
</select>

<select multiple>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
</select>

<select multiple>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
</select>

and I am looking for best solution. Wanted behaviour is when you select one item in one multi select, then remove it from the others (and conversely).

I using jquery Chosen plugin. Situation: you have 3 roles, but user can be just in one role.

jsFiddle

like image 534
Vojtech Lacina Avatar asked Nov 12 '22 00:11

Vojtech Lacina


1 Answers

basically you want to remove the option via JS/jQuery and the update the plugin.

Here is some code

  $(".chzn-select").change(function(){   
    $(".chzn-select")   
    .not($(this))  
    .find("option[value="+$(this).val()+"]")   
    .remove();   
    $(".chzn-select").trigger("liszt:updated");   
   });

and a fiddle link JsFiddle

EDIT: try this new fiddle it handles multiple select

$(".chzn-select").css('width', '300px').chosen({
        display_selected_options: false,
        no_results_text: 'not found',
    }).change(function(){
        var me = $(this);
        $(".chzn-container .search-choice").each(function(){
            var text = $('span',this).text();
            $(".chzn-select")
            .not(me)
            .find("option").each(function(){
               if ($(this).text() == text) $(this).prop('disabled',true);
            });
        });
        $(".chzn-select").trigger("liszt:updated");
});

EDITED:
try this: JsFiddle
see if it pleases you... :)

like image 76
JDuarteDJ Avatar answered Nov 14 '22 23:11

JDuarteDJ