TuanNguyen
TuanNguyen

Reputation: 71

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

Upvotes: 7

Views: 43487

Answers (6)

Seyed Ali Mahmoody
Seyed Ali Mahmoody

Reputation: 157

For RadioButtons Group

 $("input[type='radio'][value='']").prop("checked", true);

Upvotes: 0

Yogesh Gadade
Yogesh Gadade

Reputation: 1

The question is too old. But it can be useful for other people.

$( <selector> ).find('option[value="<required-value>"]').attr('selected','selected')

Upvotes: -1

Robin Rumeau
Robin Rumeau

Reputation: 61

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 :)

Upvotes: 1

Gavin M. Roy
Gavin M. Roy

Reputation: 4671

This is a bit old, but you could do:

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

Upvotes: 1

CW30Meters
CW30Meters

Reputation: 91

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

Upvotes: 3

legendofawesomeness
legendofawesomeness

Reputation: 2911

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

Upvotes: 2

Related Questions