91

I am new to jQuery and I want to enable and disable a dropdown list using a checkbox. This is my html:

<select id="dropdown" style="width:200px">
    <option value="feedback" name="aft_qst">After Quest</option>
    <option value="feedback" name="aft_exm">After Exam</option>
</select>
<input type="checkbox" id="chkdwn2" value="feedback" />

What jQuery code do I need to do this? Also searching for a good jQuery documentation/study material.

0

11 Answers 11

195

Here is one way that I hope is easy to understand:

http://jsfiddle.net/tft4t/

$(document).ready(function() {
 $("#chkdwn2").click(function() {
   if ($(this).is(":checked")) {
      $("#dropdown").prop("disabled", true);
   } else {
      $("#dropdown").prop("disabled", false);  
   }
 });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Note: jQuery 1.6.x utilizes .prop whereas earlier versions utilize .attr
$("#chkdwn2").change(function() { $("#dropdown").prop('disabled', !this.checked); }) jsfiddle.net/tft4t/74
15

I am using JQuery > 1.8 and this works for me...

$('#dropDownId').attr('disabled', true);

1 Comment

This should be the accepted answer(In early 2019) considering each loop etc have performance implications and using the attr method is the most performant way of doing it.
11

Try -

$('#chkdwn2').change(function(){
    if($(this).is(':checked'))
        $('#dropdown').removeAttr('disabled');
    else
        $('#dropdown').attr("disabled","disabled");
})

Comments

2
$("#chkdwn2").change(function(){
       $("#dropdown").slideToggle();
});

JsFiddle

1 Comment

Ill give you a thumbs up just because this is pretty cool...and I didnt know about this function.
2

To enable/disable -

$("#chkdwn2").change(function() { 
    if (this.checked) $("#dropdown").prop("disabled",true);
    else $("#dropdown").prop("disabled",false);
}) 

Demo - http://jsfiddle.net/tTX6E/

Comments

1

try this

 <script type="text/javascript">
        $(document).ready(function () {
            $("#chkdwn2").click(function () {
                if (this.checked)
                    $('#dropdown').attr('disabled', 'disabled');
                else
                    $('#dropdown').removeAttr('disabled');
            });
        });
    </script>

Comments

1

A better solution without if-else:

$(document).ready(function() {
    $("#chkdwn2").click(function() {
        $("#dropdown").prop("disabled", this.checked);  
    });
});

Comments

0
$("#chkdwn2").change(function() { 
    if (this.checked) $("#dropdown").prop("disabled",'disabled');
}) 

Comments

0
$(document).ready(function() {
 $('#chkdwn2').click(function() {
   if ($('#chkdwn2').prop('checked')) {
      $('#dropdown').prop('disabled', true);
   } else {
      $('#dropdown').prop('disabled', false);  
   }
 });
});

making use of .prop in the if statement.

Comments

0

this is to disable dropdown2 , dropdown 3 if you select the option from dropdown1 that has the value 15

$("#dropdown1").change(function(){
            if ( $(this).val()!= "15" ) {
                $("#dropdown2").attr("disabled",true);
                $("#dropdown13").attr("disabled",true);

            }

Comments

0

Your Selector should be either name of select, id (#) or class (.),

This is if you want to disable:

$("#your-selector").prop("disabled", true);

This is if you want to enable:

$("#your-selector").prop("disabled", false); 

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.