views:

29

answers:

3

Here is the basic code

<ul>
 <li>
  <input type="radio" name="r" value="a" id="a" checked="checked" />
  <label for="a">A</label>
 </li>
 <li>
  <input type="radio" name="r" value="b" id="b" />
  <label for="b">B</label>
 </li>
</ul>

So I need to have it work as:

1 Click on the <label> and check if the sibling <radio> is checked="checked" then add "selected" class to the parent <li>

2 Click on the <label> and check if the sibling <radio> is NOT checked then add checked="checked" to the sibling <radio> and add "selected" class to the parent <li> also remove all the other "checked" and "selected" in the <ul>

Could you help me please!

A: 

I'm sure this isn't the most elegant way but with a change to your markup (adding class 'a' and 'b' to your li - basically the same value as the id of the radio elem. contained) here we go:

$('label').click(function(){
    // if the radio is already checked
    if ($('#'+$(this).attr('for')).attr('checked') == 'checked') {
        $('ul li').removeClass('selected');  // remove previous selected items
        $('.'+$(this).attr('for')).addClass('selected'); // add new selected item
    } else {
        // radio not checked
        $('#'+$(this).attr('for')).attr('checked','checked'); // check radio (altough this should be automatic, without js
        $('ul li').removeClass('selected'); // clear previous selected items
        $('.'+$(this).attr('for')).addClass('selected'); // add new selected item
    }
});

For speed, I would suggest adding an id to the ul, say "list" and have the code change from

$('label').click(function(){

to

$('#list label').click(function(){

Also, from:

$('ul li').removeClass('selected');  // remove previous selected items

to

$('#list li').removeClass('selected');  // remove previous selected items

Good luck!

Claudiu
Thanks very much for that!
Ray
No worries, @Drackir's solution is much better tough, I just wrote it on the run. A +1 from me
Claudiu
+1  A: 

Clicking on the label should check the radio buttons automatically in the browser. So, you just need do add a click event to the label/input that will set the class on the li's.

Something like this should work:
HTML (I just added id="mylist")

<ul id="mylist">
 <li>
  <input type="radio" name="r" value="a" id="a" checked="checked" />
  <label for="a">A</label>
 </li>
 <li>
  <input type="radio" name="r" value="b" id="b" />
  <label for="b">B</label>
 </li>
</ul>

JavaScript

$(function() {
 $("#mylist input, #mylist label").click(function() {
  $("#mylist li").removeClass("selected");
  $(this).parent().addClass("selected");
 });
});
Drackir
you forgot the closing tags for the label, other than that, great solution
Claudiu
I'm really a css guy. so you mean it doesn't matter that wether there is check="checked" on the selected radio for passing the variable through, does it?
Ray
@Claudiu Thanks! I had some issues copying the html into the code block. I've fixed it now.
Drackir
@Ray No, what I'm saying is that the label for the radio button allows you to click on it to check/uncheck the radio button. This is one of the benefits of using the label in the first place. You can test it yourself by putting that html code into an html file (no js) and open it with your browser. When you click the label, it will check/uncheck the appropriate radio buttons.
Drackir
A: 

Since you're already using label with the for attribute there should really be no need to attach an event to the label element, unless you need to contend with IE6 making you're life difficult.

$(':radio[name="r"]').change(function(){
    $(this).parent('li').addClass('selected').siblings().removeClass('selected');
}).filter(':checked').change();

Documentation for: change()

See: http://www.jsfiddle.net/yijiang/mzPk9/

Yi Jiang
NO need IE6, thanks a lot!
Ray
@Ray At Stack Overflow, if an answer has solved your problem, you can mark the answer as accepted with the green checkmark on the side. This will mark your question as solved and give both the answerer and the asker a small reward for their efforts :)
Yi Jiang