Select (HTML) with multiple selection. Marco one and should mark all, not working

2

I have this select with multiple select

<tr>
                                <td width="10%" class="label_right">Autorização Prévia: &nbsp;</td>
                                <td class="label_left">
                                    <select id="ddl_autorizacaoprevia" multiple>
                                        <option value="0">TODAS AS AUTORIZAÇÕES</option>
                                        <option value="T">TÉCNICA / ADMINISTRATIVA</option>
                                        <option value="A">SISTÊMICA</option>
                                        <option value="N">NÃO PRECISA</option>
                                    </select>
                                </td>
                            </tr>

What I want is that when I select " ALL AUTHORIZATIONS ", all others should be selected and when I click on any other, all selected should be cleared and only the selected should be marked (selected). I really do not know how to do this. I'm searching the internet, but so far I have not found anything.

    
asked by anonymous 19.01.2016 / 18:49

1 answer

1

With jquery it would look something like this:

$(document).ready(function(){

   $('#ddl_autorizacaoprevia option').click(function(){
       var that = $(this);
       if ($(that).val() == 0){
          $('#ddl_autorizacaoprevia option').each(function(){
             $(this).attr('selected', 'selected');
          });
       }  else  {
          $('#ddl_autorizacaoprevia option').each(function(){
             $(this).removeAttr('selected'); 
          });
          $(that).attr('selected', 'selected');
       }

   }

});

Something in this line should solve your problem

    
19.01.2016 / 19:03