I am working on this project that has a reservation form which allow users specify which sort of service they require. I have a select menu with eight (8) options and I have two divs which would either show/hide depending on the value of the option been selected. Seen below is the select element:
<select name="serviceChoice" id="serviceChoice">
<option value="A">Option A</option>
<option value="B">Option B</option>
<option value="C" selected="selected">Option C</option>
<option value="D">Option D</option>
<option value="E">Option E</option>
<option value="F">Option F</option>
</select>
<div id="field_1" class="optionBox">I have several form fields</div>
<div id="field_2" class="optionBox">I have several form fields</div>
I have written my jQuery code to alternate between these divs, however I would like to know if there is a shorter better way to get this done. I would also like to clear the values of any input elements in a div which has been hidden due to the choice of the user. My code is as follows:
(function($){
$('#field_1').show();
$('select[name=serviceChoice]').change(function(){
if( $('select[name=serviceChoice] option:selected').val() == 'A' ) {
$('.optionBox').hide();
$('#dropOff').show();
} else if ( $('select[name=serviceChoice] option:selected').val() == 'B' ) {
$('.optionBox').hide();
$('#pickUp').show();
} else if ( $('select[name=serviceChoice] option:selected').val() == 'C' ) {
$('.optionBox').hide();
$('#pickUp').show();
} else if ( $('select[name=serviceChoice] option:selected').val() == 'D' ) {
$('.optionBox').hide();
$('#pickUp').show();
} else {
$('.optionBox').show();
}
});
})(jQuery);