I am new to jQuery and I want to enable and disable a dropdown list using a checkbox. This is my html:
Try -
$('#chkdwn2').change(function(){
if($(this).is(':checked'))
$('#dropdown').removeAttr('disabled');
else
$('#dropdown').attr("disabled","disabled");
})
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);
}
});
});
A better solution without if-else:
$(document).ready(function() {
$("#chkdwn2").click(function() {
$("#dropdown").prop("disabled", this.checked);
});
});
try this
<script type="text/javascript">
$(document).ready(function () {
$("#chkdwn2").click(function () {
if (this.checked)
$('#dropdown').attr('disabled', 'disabled');
else
$('#dropdown').removeAttr('disabled');
});
});
</script>