Reputation: 71
<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
Reputation: 157
For RadioButtons Group
$("input[type='radio'][value='']").prop("checked", true);
Upvotes: 0
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
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
Reputation: 4671
This is a bit old, but you could do:
$('#category').find('option:empty')
Upvotes: 1
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