javascript - jQuery Multi Select Option - Check if a option is selected or not -


i have html multi select box

<form action="form_action.php" method="post"> <select name="cars[]" multiple id="select_id">     <option value="all" id="option_id">all</option>     <option value="volvo">volvo</option>     <option value="saab">saab</option>     <option value="opel">opel</option>     <option value="audi">audi</option> </select> <input type="submit"> </form> 

what trying check if option "all" selected or not in jquery.

what have tried

<script>         $(document).ready(function()         {             $('#option_id')                 .click(                     function()                     {                         if ($("#select_id option[value='all']").length == 0) { //value not exist add                             //do if not selected                         }                         else                         {                             //do if selected                         }                         //$("#select_id option:selected").removeattr("selected");           //remove                         //$("#select_id option").attr('selected', 'selected');      //add                     }                 );         });     </script> 

but not working.

can me please?

thanks in advance helping.

please below code

$('#option_id').click(function(){ if ($("#select_id option[value=all]:selected").length > 0){     alert('all selected'); } else{     //do if not selected } //$("#select_id option:selected").removeattr("selected");           //remove //$("#select_id option").attr('selected', 'selected');      //add }); 

you can check below fiddle

multi select


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -