SumoSelect Unselect the other option

1.3k Views Asked by At

I want to unselect all the other option when i selected the option with value 0. On the other way, I need to unselect the option with value 0 when i select other value that is not 0. Please help me, below are my code.

<script type="text/javascript">
    $(document).ready(function() {
       $('.sumomultiple').SumoSelect();
       $('.sumomultiple')[0].sumo.selectItem("0");
   });
</script>

<select id="st_type" class="sumomultiple table-group-action-input form-control" multiple="multiple" placeholder="Unselect All" onchange="$('#s_transtype').val($(this).val())">
    <option value="0">Unselect All</option>
    <option value="23">A</option>
    <option value="24">B</option>
    <option value="25">C</option>
</select>

$('#st_type').change(function(){

 // Please Help Here

});

I already try many ways i still have no idea how to do it, PLease help me. Thank you very much...

1

There are 1 best solutions below

2
Rory McCrossan On

This behaviour is built in to SumoSelect, there is no need to write it yourself. Use the selectAll option in the settings to enable it:

jQuery(function() {
  $('.sumomultiple').on('change', function() {
    $('#s_transtype').val($(this).val())
  }).SumoSelect({
    selectAll: true
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.sumoselect/3.0.2/jquery.sumoselect.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.sumoselect/3.0.2/sumoselect.min.css" />
<select id="st_type" class="sumomultiple table-group-action-input form-control" multiple="multiple" placeholder="Unselect All">
  <option value="23">A</option>
  <option value="24">B</option>
  <option value="25">C</option>
</select>

<input type="text" readonly="true" id="s_transtype" />

I'd suggest reading the library documentation and viewing the examples to understand more about the features available.