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/

1
  • just use $('#dw').on('change', function() { Commented Jun 1, 2012 at 5:46

4 Answers 4

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>
​
Sign up to request clarification or add additional context in comments.

Comments

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()

Comments

0

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.