Norse Norse - 3 months ago 11
jQuery Question

Jquery disable element on checkbox change

<input type="checkbox" id="dw"/>

<select id="oo">
<option>a</option>
<option>b</option>
<option>c</option>
</select>

$('#dw').change(function() {
if ($(this).is(':checked')) {
$('#oo').removeAttr('disabled');
} else {
$('#oo').attr('disabled', true);
}
}


I don't see where the error would be.

http://jsfiddle.net/f8jGU/

Answer

Working demo http://jsfiddle.net/2gbHQ/5/

Good Read http://api.jquery.com/prop/

The .prop() method gets the property value for only the first element in the matched set. It returns undefined for the value of a property that has not been set, or if the matched set has no elements. To get the value for each element individually, use a looping construct such as jQuery's .each() or .map() method.

Jquery code

$('#dw').change(function() {
    if ($(this).is(':checked')) {
        $('#oo').prop('disabled', false);
    } else {
        $('#oo').prop('disabled', true);
    }
});​

HTML

<input type="checkbox" id="dw" />

<select id="oo" disabled="true">
    <option>a</option>
    <option>b</option>
    <option>c</option>
</select>
​
Comments