1
<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/

Norse
  • 5,674
  • 16
  • 50
  • 86

4 Answers4

1
$('#dw').on('change', function() {

    // If this.checked = true, then select will enabled else disabled
    // instead of attr() use prop()
    $('#oo').prop('disabled', !this.checked);
});

Working sample

Read more about .prop()

You can gain huge from below link:

.prop() vs .attr()

Community
  • 1
  • 1
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
1

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>
​
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
0

Your code also works except that you missed to close the change function. Please see the updated fiddle http://jsfiddle.net/f8jGU/1/

raddykrish
  • 1,866
  • 13
  • 15
0

The reason your code didn't work is explained in the jQuery documentation for .prop() (https://api.jquery.com/prop/):

Properties generally affect the dynamic state of a DOM element without changing the serialized HTML attribute. Examples include the value property of input elements, the disabled property of inputs and buttons, or the checked property of a checkbox. The .prop() method should be used to set disabled and checked instead of the .attr() method. The .val() method should be used for getting and setting value.

1  $( "input" ).prop( "disabled", false );
2  $( "input" ).prop("checked", true );
3  $( "input" ).val( "someValue" );

Important: the .removeProp() method should not be used to set these properties to false. Once a native property is removed, it cannot be added again. See .removeProp() for more information.

Daniel Gray
  • 1,697
  • 1
  • 21
  • 41