Disable radio buttons based on value

Ajay picture Ajay · Sep 17, 2011 · Viewed 24.4k times · Source

I want to disable radio buttons based on the value of a variable. The radio that is equal to the variable value should be disabled.

Ex:

<input type="radio" name="r1" value="a" />Value a
<input type="radio" name="r1" value="b" />Value b

So if the $variable = 'a'; then the radio button which has the value a should be disabled.

Answer

ipr101 picture ipr101 · Sep 17, 2011

Try -

var a = 'a';
$("input[type=radio][value=" + a + "]").prop("disabled",true);

or

var a = 'a';
$("input[type=radio][value=" + a + "]").attr("disabled",true);

If you're using an older jQuery version.

Working demo - http://jsfiddle.net/FtwcL/1