I have this: http://jsfiddle.net/ptWhn/
<pre>
/*HTML*/
`<label class="for_radio"><input type="radio"><span>radio-button-1</span></label>`
`<label class="for_radio"><input type="radio"><span>radio-button-2</span></label>`
/*jQuery*/
$(document).ready(function(){
$('input[type="radio"]').click(function() {
if($('input[type="radio"]').is(':checked')) {
$(this).closest('label').addClass('active');}
else {
$(this).closest('label').removeClass('active');
}
});
});
/*CSS*/
.active {
background: #ffc;
}
</pre>
... which works in the sense that the label's background color changes when the radio button is selected, except that I can't figure out how to remove the .active class which changes the background color when one of the radio buttons is UNchecked. And also in jsfiddle, for some reason, the alternate radio button doesn't de-select when the other one is clicked.
The second thing I'm having trouble figuring out why is why I seem to need the .click(function() before the if statement. That is,
if($('input[type="radio"]').is(':checked')) {
$(this).closest('label').addClass('active');}
... doesn't work by itself as a JS snippet. Why?
Would appreciate any insight. Thanks.
<pre>!=<script>, right? You're not going to run anything in <pre> tags, you're just going to print it out with code-like formatting. EDIT: this looks like actually just some weird kind of formatting jsfiddle applied to your code. Will check your js.