Search code examples
jqueryselectedvalue

Check if some attr are checked with jquery on load


I would like to check if 2 specifics out of 3 options in my html select are checked on load.

Actually, my HTML :

<select name="country" id="country" tabindex="7" style="width:128px; margin-right:26px;">
            <option selected="selected" name="0">Choisir...</option>
            <option value="Canada" <?php if($pays == 'Canada'){echo "selected=selected";} ?>>Canada</option>
            <option value="États-Unis" <?php if($state == 'États-Unis'){echo "selected=selected";} ?>>États-Unis</option>
        </select>

And My jQuery is the following :

if($('#reference').attr('selected',true))
    {
        $('.showDetaillant').show();
    }

As you may see, it only check is something is "selected". By default, this is the option "Choisir...".

Want I am looking for, is to set .hide() if the option "Choisir..." is selected.

Thx


Solution

  •     <select name="country" id="country" tabindex="7" style="width:128px; margin-
            right:26px;">
                 <option selected="selected" value="0">Choisir...</option>
         </select>
    
    <script>
         $(document).ready(function () {
    
             if($('#country').val()=="0")
               $('.showDetaillant').show();
           });
    
    <script>