What is a simple jquery way to select a radio when checkbox is checked and then deselect the radio when checkbox unchecked?

StackOverflow https://stackoverflow.com/questions/1269349

Question

<input id="myCheckbox" type="checkbox" /> Please, please check me
    <input id="myRadio" type="radio" /> Am I selected ?

I would like the following behavior:

  1. When myCheckbox is checked then myRadio is going to get selected.
  2. When myCheckbox is unchecked then myRadio is going to become unselected.

How will I do this in a very simple way ?

Thank you in advance for your effort to respond.

Was it helpful?

Solution

$('#myCheckbox').click(function() {
    if ($(this).is(':checked')) {
        $('#myRadio').attr('checked', 'checked');
    } else {
        $('#myRadio').removeAttr('checked');
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top