user3108698
user3108698

Reputation: 719

jQuery: Hide Show div when checkbox is checked or unchecked

Hi I want a div to be hidden when the page load and then if a checkbox is checked, show the div and then hide again when it is unchecked.

.checkCover is the checkbox and #show_coversheet is the div.

What is wrong with the below code?

$(function () {
  $('.checkCover').change(function () {                
     $('#show_coversheet').toggle(!this.unchecked);
  }).change();
});

Upvotes: 0

Views: 1903

Answers (2)

user3108698
user3108698

Reputation: 719

Take out the ! before this.checked

$('.checkCover').change(function () {                
    $('#show_coversheet').toggle(this.checked);
}).change();

Upvotes: 1

tymeJV
tymeJV

Reputation: 104775

unchecked is not a property, but checked is!

$('.checkCover').change(function () {                
    $('#show_coversheet').toggle(!this.checked);
}).change();

Upvotes: 3

Related Questions