There are several ways you could refactor that code. Here's one approach:
 
    $('#normal, #standard, #profi').click( function() {
        var idnums = { normal:1, standard:2, profi:3 };
        $('#bereit').ScrollTo( 800 );
        for( var id in idnums ) {
            var n = idnums[id];
            var method = 'removeClass', checked = '';
            if( id == this.id ) method = 'addClass', checked = 'checked';
            $( '#' + id + ' dd, #label_radiobutton_' + n )[method](
'ausgewaehlt' );
            $( '#radiobutton_' + n ).attr( 'checked', checked );
        }
    });

-Mike


  _____  

From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Dominik Hahn
Sent: Monday, February 12, 2007 9:14 AM
To: [email protected]
Subject: [jQuery] how to simplify this


Hello,

I am looking for a way to simplify this chunk of code:


        $('li#normal').click(function() {
            $('#bereit').ScrollTo(800);
            $('li#normal dd').toggleClass('ausgewaehlt'); 
            $('li#standard dd').removeClass('ausgewaehlt');
            $('li#profi dd').removeClass('ausgewaehlt');
            
            $('label#label_radiobutton_1').toggleClass('ausgewaehlt'); 
            $('label#label_radiobutton_2').removeClass('ausgewaehlt');
            $('label#label_radiobutton_3').removeClass('ausgewaehlt');
            
            $('input#radiobutton_1').attr('checked','checked'); 
            $('input#radiobutton_2').attr('checked','');
            $('input#radiobutton_3').attr('checked','');
        });
        $('li#standard').click(function() { 
            $('#bereit').ScrollTo(800);
            $('li#normal dd').removeClass('ausgewaehlt');
            $('li#standard dd').toggleClass('ausgewaehlt');
            $('li#profi dd').removeClass('ausgewaehlt'); 
            
            $('label#label_radiobutton_1').removeClass('ausgewaehlt');
            $('label#label_radiobutton_2').toggleClass('ausgewaehlt');
            $('label#label_radiobutton_3').removeClass('ausgewaehlt'); 
            
            $('input#radiobutton_1').attr('checked','');
            $('input#radiobutton_2').attr('checked','checked');
            $('input#radiobutton_3').attr('checked',''); 
        });
        $('li#profi').click(function() {
            $('#bereit').ScrollTo(800);
            $('li#normal dd').removeClass('ausgewaehlt');
            $('li#standard dd').removeClass('ausgewaehlt'); 
            $('li#profi dd').toggleClass('ausgewaehlt');
            
            $('label#label_radiobutton_1').removeClass('ausgewaehlt');
            $('label#label_radiobutton_2').removeClass('ausgewaehlt'); 
            $('label#label_radiobutton_3').toggleClass('ausgewaehlt');
            
            $('input#radiobutton_1').attr('checked','');
            $('input#radiobutton_2').attr('checked',''); 
            $('input#radiobutton_3').attr('checked','checked');
        });


It controls a list, when you click on one of the three items, it scrolls
down to a online form (first line), the item is highlighted (first three
lines), one of three radioboxes is checked (line 6-8) and the label for the
checked radiobox is also highlighted (last three lines). 


Can you help me?

Thanks,
Dominik


_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to