[jQuery] Optimized Code
[jQuery] Optimized Code
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');
}
});
}
);