Patrick Schomburg Patrick Schomburg - 1 month ago 8
jQuery Question

How to check and select radio buttons by title using jquery

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");

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


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.

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: