jQuery Form How to - Disable a textbox if a checkbox is checked








Question

We would like to know how to disable a textbox if a checkbox is checked.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.10.0.js'></script>
<script type='text/javascript'>
$(function () {<!--from w w w.  j  a v a  2  s. c o  m-->
    $('.ba').on('change', function () {
        var checked = $(this).prop('checked');
        $('.tex').prop('disabled', !checked);
    });
});

</script>
</head>
<body>
  <input type="checkbox" class="ba" checked="checked" />
  <input type="text" class="tex" />
</body>
</html>

The code above is rendered as follows: