Reputation: 27038
I have this form select:
<select name="select_date" id="select_date">
<optgroup label="Select Date">
<option value="0" selected="selected">Custom Date</option>
</optgroup>
<optgroup label="Or Custom Date">
<option value="1"> Current Month</option>
<option value="2"> Last Month</option>
<option value="3"> Last 3 Months</option>
<option value="4"> Last 6 Months</option>
<option value="5"> Last Ever</option>
</optgroup>
</select>
<div id="test">hide me</div>
How can I check for a selected option so that: if option 3 selected then hide the div with id test
?
Upvotes: 1
Views: 3835
Reputation: 11044
$('#select_date').change(function(){
if ($('option:selected', this).val() == 3) $('div#test').hide();
});
Edit: Bonus: working fiddle
Upvotes: 1
Reputation: 12509
$('#select_date').change(function() {
$('#test').show();
if($(this).val() === '3') {
$('#test').hide();
}
});
Upvotes: 2
Reputation: 126042
$("#test").toggle($("#select_date").val() != 3);
Wrapped in an event handler for the change
event:
$("#select_date").bind("change", function () {
$("#test").toggle(this.value != 3);
}).change();
Example: http://jsfiddle.net/Qde8d/
Notes:
toggle
that takes a showOrHide
parameter to show #test
when the selected option is not 3, and hide it when it is 3.select
element using this.value
inside the event handler or .val()
if you're working with a jQuery object.Upvotes: 4