Don't run functions that return the same result multiple times, run them once and store the results in a variable. Example:
$(document).ready () { var checkedVal = $('input:checked').val(); if (checkedVal = 'city') { // .... } } If you need to run more than 1 operation on a jQuery object then use chaining or copy the object to a variable and run the operations on the var. Wrong: $('input').addClass ('error'); $('input').after ('This field is required'); Right: $('input').addClass ('error').after ('This field is required'); Or var inputs = $('input'); inputs.addClass ('error'); inputs.after ('This field is required'); On Dec 15, 9: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'); > } > }); > } > );