vendredi 29 mai 2015

Cannot deselect all optionsof a multi-select if the option "All" is selected

Example Fiddle

I am having this problem(the dropdown is using chosen pluggin)

I am calling a jquery function. The issue is, there is a multiselect with the first option being "All" and all the other options are present

When the "All" will be selected, then all the other options will be deselected.

Here's my multi-select:

    <select id="risk_mitigator_offer_1" name="risk_mitigator_offer_1[]" 
       class="input_field criteria_risk" style="width:302px;" 
       multiple onchange="checkAll(this)">
       <option value="All">All</option>
       <?php
       $offers_off = array();
       $_sql_off = mysql_query( 'SELECT * FROM mt_offers WHERE camp_id=' . $mysql['camp_id'] . ' AND deleted = "0" ORDER BY offer_id' );
       if ($_sql_off) 
       {
         while ($_data_off = mysql_fetch_assoc( $_sql_off )) 
         {?>
            <option value="<?php echo $_data_off['offer_id']?>"><?php echo $_data_off['offer_name'];?></option>
   <?php  }
       } ?>  
    </select>

And here's is the jquery function:

function checkAll(obj)
{
    var values = $(obj).val();
    if (values) 
    {
        for (var i = 0; i < values.length; i++) 
        {
            if(values[i] == "All")
            {
                $(obj+" option[value!=All]").removeAttr("selected");
            }
        }
    }
}

Now the problem is that,I am getting the following jquery error:

Error: Syntax error, unrecognized expression: [object HTMLSelectElement] option[value!=All]


throw new Error( "Syntax error, unrecognized expression: " + msg );

How can I fix this issue?

I know it may be a silly one,the issue is in this line of code:

$(obj+" option[value!=All]").removeAttr("selected");

Aucun commentaire:

Enregistrer un commentaire