2

I have run into a problem that I am unsure of how to resolve. According to my debugging using firefox and simple console logs I am doing it correctly. I can see the specified div id for the selected radio button toggle when I select each button. However when using the addClass() funcion the specified class is not getting applied to the selected div id for the checked radio button. Whew what a mouthful.

<script type="text/javascript">
 var $j = jQuery.noConflict();
 $j(document).ready(function(){
  $j('#div :radio').buttonset().click(function(){
   $j(this).find(':checked').attr('id').addClass('');
   $j(this).find(':checked').attr('id').addClass('checked');
  });
 });
</script>

<style>
 .checked{background:url(icon.png) left no-repeat;}
</style>

<div id="div">
 <p><input type="radio" id="radio-1" name="radio" value="radio-1" /><label for="radio-1">Radio-1</label></p>
 <p><input type="radio" id="radio-2" name="radio" value="radio-2" /><label for="radio-2">Radio-2</label></p>
 <p><input type="radio" id="radio-3" name="radio" value="radio-3" /><label for="radio-3">Radio-3</label></p>
</div>
Jonas
  • 121,568
  • 97
  • 310
  • 388
jas-
  • 1,801
  • 1
  • 18
  • 30

1 Answers1

2

.attr() returns a string, and your code calls .addClass() on that string. It should be obvious that won't work. You don't want to add the class to the ID — in fact you can't, because there is no String.addClass method.

Try this:

$j('#div').delegate('input[type="radio"]', 'change', function ()
{
    $j(this).parent().toggleClass('checked', this.checked);
});

Edit

Is this more like what you're looking for? http://jsfiddle.net/mattball/ACREd/

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 1
    I'm pretty sure he's talking about the labels next to the radios (as per the title of the question) but I could be wrong. – karim79 Aug 19 '11 at 03:04
  • I'm not clear on which element you actually want to add the class to, so I took a guess and added it to the checked input's parent `

    `. (cc @karim)

    – Matt Ball Aug 19 '11 at 03:05
  • 1
    Guessing games make up half the fun anyway at SO if you ask me. – karim79 Aug 19 '11 at 03:06
  • 1
    If by "fun" you mean "aggravation wherein I look at a question, say 'I know how to answer this, but it's just not worth the frustration of trying to communicate with the OP' and move on" ...then yes `:P` – Matt Ball Aug 19 '11 at 03:08
  • I have never used the delegate function nor did I know it existed. Thanks for the heads up on the .attr('id') returning a string. I knew that, just haven't had to traverse the dom for awhile. Now your solution works, however it replaces the current class assigned, how can I append to the existing class? This is another thing I have never done. Thanks. – jas- Aug 19 '11 at 03:31
  • @jas use `.addClass('checked')` instead of `.toggleClass(...)`. – Matt Ball Aug 19 '11 at 03:34
  • How can I append the 'checked' class to the existing class for the element? The removeClass, and toggleClass is not removing the other radio buttons class. – jas- Aug 19 '11 at 03:45
  • I do not understand what you mean. When should the `checked` class be added to which element, and when should that class be removed from which elements? – Matt Ball Aug 19 '11 at 03:47
  • Once the 'checked' attribute is set. I have tried... `$j(this).find(':checked').removeClass('checked');` – jas- Aug 19 '11 at 03:48
  • Sorry, that's just not clear to me. Do you mean that selecting the radio button should add the `checked` class (and to which element)? – Matt Ball Aug 19 '11 at 03:57
  • I appreciate the help. Three radio buttons which have an existing UI class applied as seen [here](http://jqueryui.com/demos/button/#radio). Once one of the radio buttons are selected I want to 'add' the 'checked' class to the radio button which has had the `(':checked')` attribute set. I hope that is a bit more clear. – jas- Aug 19 '11 at 03:59
  • See my edit... if that's still not right, I can't help if you can't communicate what you're trying to achieve. – Matt Ball Aug 19 '11 at 04:02
  • That is close enough. Thanks you just showed me of two functions in jquery I never knew existed. – jas- Aug 19 '11 at 04:06