I am new to jQuery and I want to enable and disable a dropdown list using a checkbox. This is my html:
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);
}
$(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.
To enable/disable -
$("#chkdwn2").change(function() {
if (this.checked) $("#dropdown").prop("disabled",true);
else $("#dropdown").prop("disabled",false);
})
Demo - http://jsfiddle.net/tTX6E/
I am using JQuery > 1.8 and this works for me...
$('#dropDownId').attr('disabled', true);
$("#chkdwn2").change(function(){
$("#dropdown").slideToggle();
});
JsFiddle
$("#chkdwn2").change(function() {
if (this.checked) $("#dropdown").prop("disabled",'disabled');
})