Here's how I will code it. jQuery(function($) { // this means document.ready
var optionDisplay = function() { var val = $("input:checked").val(); $(".city, .zip, .country").hide(); if (val.length > 0) $("."+val).show(); else $(".city").show(); $("#id_zip_code, #id_country, #id_city").find('option:first').attr ('selected', 'selected'); } optionDisplay(); // run optionDisplay on document ready $('input').click(optionDisplay); }); On Dec 15, 1:55 pm, issya <floridali...@gmail.com> wrote: > I recently made this small script and was thinking it is a bit long. > It works just fine, I was wondering if there is any way to make it > shorter or is this right? Thanks in advance for the help. > > $(document).ready( > function() > { > if ($('input:checked').val() == 'city') { > $('.city').show(); > $('.zip').hide(); > $('.county').hide(); } > else if ($('input:checked').val() == 'zip') { > $('.zip').show(); > $('.city').hide(); > $('.county').hide(); } > else if ($('input:checked').val() == 'county') { > $('.county').show(); > $('.zip').hide(); > $('.city').hide(); } > else { > $('.city').show(); > $('.zip').hide(); > $('.county').hide(); } > > $('input').click(function() { > if ($(this).val() == 'city') { > $('.city').show(); > $('.zip').hide(); > $('.county').hide(); > > $('#id_zip_code').find('option:first').attr('selected', > 'selected').parent('select'); > > $('#id_county').find('option:first').attr('selected', > 'selected').parent('select'); > } > else if ($(this).val() == 'zip') { > $('.zip').show(); > $('.city').hide(); > $('.county').hide(); > > $('#id_city').find('option:first').attr('selected', > 'selected').parent('select'); > > $('#id_county').find('option:first').attr('selected', > 'selected').parent('select'); > } > else if ($(this).val() == 'county') { > $('.county').show(); > $('.city').hide(); > $('.zip').hide(); > > $('#id_zip_code').find('option:first').attr('selected', > 'selected').parent('select'); > > $('#id_city').find('option:first').attr('selected', > 'selected').parent('select'); > } > }); > } > );