Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery get select option value empty

<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!!!

like image 463
TuanNguyen Avatar asked Oct 24 '11 19:10

TuanNguyen


2 Answers

if(!$("#category").val())
     //selected valule is ""
like image 86
CW30Meters Avatar answered Sep 28 '22 11:09

CW30Meters


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 -->
}
like image 25
legendofawesomeness Avatar answered Sep 28 '22 10:09

legendofawesomeness