views:

186

answers:

1

I want to have a group of radiobuttons that are inactive until a checkbox is clicked.

In other words, when the user checks a checkbox, it will allow the user to use the radiobuttons. If the user unchecks a checkbox, it will make the radiobuttons inactive, so the user cannot select them anymore.

Is there a solution using jQuery that I can use to achieve this?

+5  A: 

Assuming that the radiobuttons have all the same name, then you can use the attribute selector for this:

$('#checkboxid').click(function() {
    $('input[name="radiobuttonname"]').attr('disabled', !this.checked);
});

This will enable the radiobuttons if the checkbox is checked and disable when unchecked.

BalusC
That is perfect! Exactly what I was looking for. Thank you! What does !this.checked do? I can't find any documentation for that. Also, how would I grey out any text that is with the radiobuttons using the toggle switch?
zeckdude
The `this` refers to the element on which the `click` event is fired, thus the checkbox itself. The `this.checked` returns boolean `true` if the checkbox is checked. Also see http://wwd.w3schools.com/jsref/dom_obj_checkbox.asp. The `!` just inverses the boolean so that for example `true` becomes `false`. You can style text with help of CSS and `toggleClass()` in jQuery. Also see http://docs.jquery.com/Attributes/toggleClass Assuming that those text is presented by `<label>`, you can get them by looping over radiobuttons and use `$('label[for="' + this.id + '"]')`.
BalusC
Thanks again for your help with the ToggleClass. Worked wonderfully!
zeckdude