Search code examples
jqueryradio-buttonlabelselected

Assign "selected" to radio label with jquery


I have a group of radio inputs with associated labels. There is some CSS magic to pretty up the buttons, and this works fine. I need to be able to change the selected attribute of the label.

<input type='radio' id='radio1' name='resolution' value='0' selected />
<input type='radio' id='radio2' name='resolution' value='1' />
<label for='radio1' class='cb-enable selected' ><span>Open</span></label>
<label for='radio2' class='cb-disable ' ><span>Closed</span></label>

How would I go about setting the label selector with jQuery?


Solution

  • $("input:radio[name='resolution']").change(function(){
        $("label").removeClass("selected");
        $("label[for='" + this.id + "']").toggleClass("selected", this.checked);
    });
    

    http://jsfiddle.net/QKL5t/