jQuery get select option value empty

35,878

Solution 1

if(!$("#category").val())
     //selected valule is ""

Solution 2

I am not sure if I understand your question correctly, but are you looking to do something like this?

if($('#category option:selected')){
     var selValue = $('#category').val();
     var category_url;

     if(selValue)
         category_url = "&category="+$('#category').val();    <!-- True -->
     else
         <!-- error -->
}

Solution 3

This is a bit old, but you could do:

$('#category').find('option:empty')

Solution 4

In fact, if you tell the browser to select nothing, it does the jox :

$("#category option").attr("selected", false); //Unselect each option

By default the browser Will select will show the first option, whatever is the option's value.

Hope this will helps :)

Share:
35,878
Admin
Author by

Admin

Updated on October 04, 2020

Comments

  • Admin
    Admin over 3 years
    <select name="category" id="category">
        <option value="">All Category</option>
        <option value="1">Category 1</option>
        <option value="2">Category 2</option>
        <option value="3">Category 3</option>
    
    </select>
    

    How to choose the category with value=""

    Conditions for a correct:

    if($('#category option:selected')){
        var category_url = "&category="+$('#category').val();    <!-- True -->
    }
    

    Condition 2 wrong:

    if($('#category option[value=""]')){
        var category_url = "";                              <!-- Error  -->
    }
    

    Please help me!!!