0

I've attempted with the following code, to check if three radio buttons are checked, then check one if none in the set are:

if ($('input[title="a"]:selected').length == 0 
        && $('input[title="b"]:selected').length == 0
        && $('input[title="c"]:selected').length == 0
    ) {
        console.log("Nothing checked");
        $('input[title="b"]').prop("checked",true);
    }
}

it logs in the console every time (regardless of their states) and doesn't check the radio button.

martin
  • 93,354
  • 25
  • 191
  • 226
Patrick Schomburg
  • 2,494
  • 1
  • 18
  • 46

1 Answers1

1

You probably don't want to use :selected. jQuery doc says:

The :selected selector works for elements. It does not work for checkboxes or radio inputs; use :checked for them.

so use :checked instead. https://api.jquery.com/checked-selector/

Checking the radio button is explained here How to check a radio button with jQuery ? but the problem is probably somewhere else.

Are you sure the attribute selector matches the input? Try dumping its results because I guess it doesn't:

 console.log($('input[title="b"]'));
Community
  • 1
  • 1
martin
  • 93,354
  • 25
  • 191
  • 226