views:

42

answers:

1

Hey guys. I'm not much of a programmer, but still need to do some coding.

Now I have a problem with changing Radio Button Image with jQuery. I've got this code of the web but it still doesn't work. It changes the Radio button images, but when I try to select, it changes only the picture of the first radio box.(every time)

Here's my HTML code:

   <div id="testcontent">
<div class="questions">
<div id="question0" class="question">
<span class="qutitle">asdg asdg</span>
<FIELDSET class="radios">
<label for="q_0" class="label_radio">
<input id="q_0" type="radio" name="q_0" value="2">dgasdg
</label>
<label for="q_0" class="label_radio">
<input id="q_0" type="radio" name="q_0" value="0"> sdgas 
</label>
<label for="q_0" class="label_radio">
<input id="q_0" type="radio" name="q_0" value="1">dgas 
</label>
</FIELDSET>
</div>

Here's the css. I hope I got it right.

#testcontent .label_check,
#testcontent .label_radio    { padding-left: 34px; }
#testcontent .label_radio    { background: url(radio-off.png) no-repeat; }
#testcontent .label_check    { background: url(check-off.png) no-repeat; }
#testcontent label.c_on      { background: url(check-on.png) no-repeat; }
#testcontent label.r_on      { background: url(radio-on.png) no-repeat; }
#testcontent .label_check input,
#testcontent .label_radio input  { position: absolute; left: -9999px; }

And here's the jquery code.

 function setupLabel() {
        if ($('.label_check input').length) {
            $('.label_check').each(function(){ 
                $(this).removeClass('c_on');
            });
            $('.label_check input:checked').each(function(){ 
                $(this).parent('label').addClass('c_on');
            });                
        };
        if ($('.label_radio input').length) {
            $('.label_radio').each(function(){ 
                $(this).removeClass('r_on');
            });
            $('.label_radio input:checked').each(function(){ 
                $(this).parent('label').addClass('r_on');
            });
        };
    };
    $(document).ready(function(){
        $('.label_check, .label_radio').click(function(){
            setupLabel();
        });
        setupLabel(); 
    });

Please tell me what I is wrong with this code, help is much appreciated.

Thank you.

A: 

I really recommend using jquery.uniform (since you are already using jQuery, or any equal alternative) for styling form elements like checkbox, radio and select. There is a lot more to it than just placing an image above the 'real' form-element, like handling clicks on the label etc.

zeroathome
Thanks, that was much easier.
Bndr