0

I am checking if a checkbox is checked, and if so, setting the visibility of a DIV.

However, the property check is failing. Nothing happens. When I put an alert before the "if", the alert fires. But with the if's, nothing happens. What am I doing wrong?

$('.cbFee').click(function () {
    if ($('.cbFee').prop('checked')) {
        $('.grpAnnualFee').hide();
    } else {
        $('.grpAnnualFee').show();
    }
});
Craig
  • 18,074
  • 38
  • 147
  • 248

2 Answers2

1

Try This :-

$('.cbFee').click(function () {
    if ($(this).is(':checked')) {   
        $('.grpAnnualFee').hide();
    } else {
        $('.grpAnnualFee').show();
    }
});

OR

$('.cbFee').click(function () {
    if (this.checked) {   
         $('.grpAnnualFee').hide();
    } else {
         $('.grpAnnualFee').show();
    }
});

and while dealing with checkboxes it is better to use .change() instead of .click().

Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69
1

You can use $('.cbFee').is(':checked')

user700284
  • 13,540
  • 8
  • 40
  • 74