Thank you for all of the great replies. I am going to implement some
of this code now. As I mentioned before, It is always great to see how
others would do something. Especially when I am newer to jQuery.

On Dec 16, 8:40 am, Ricardo Tomasi <ricardob...@gmail.com> wrote:
> I'd write it like this:
>
> $(document).ready(function(){
>
>    function hideOthers(){
>       var sel = $('div.'+ $(what).val() || 'city' );
>       $('div').not(sel).hide();
>       sel.show();
>    };
>    hideOthers('input:checked');
>
>    $('input').click(function(){
>       hideOthers(this);
>       $('select[id!='+ $(this).val() +'] option:first-child').attr
> ('selected','selected');
>    });
>
> });
>
> Where 'div' is the selector for your '.city, .zip, .country' elements,
> or '.city, .zip, .country' if you don't need flexibility.
>
> In case .city, .zip and .country are siblings it's even easier:
>
> $(document).ready(function(){
>
>    var sel = $('input.checked').val() || 'city';
>    $('.'+val).show().siblings().hide();
>
>    $('input').click(function(){
>       $('.'+$(this).val()).show().siblings().hide();
>       $('select[id!='+ $(this).val() +'] option:first-child').attr
> ('selected','selected');
>    });
>
> });
>
> - ricardo
>
> On Dec 15, 7: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');
> >                 }
> >                 });
> >         }
> > );
>
>

Reply via email to