Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle div based on checkbox value

Tags:

jquery

I'm not sure how to accomplish this. What I want to do is to hide a div based on a checkbox value. This is my code for the toggle.

appreciate any help

.always is the checkbox

$(document).ready(function() {
  $('.always').click(function() {
    $('#dates').toggle();
  });
});
like image 610
Dejan.S Avatar asked Dec 02 '10 16:12

Dejan.S


2 Answers

.toggle() takes a boolean as well, like this:

$(function () {
  $('.always').change(function () {                
     $('#dates').toggle(!this.checked);
  }).change(); //ensure visible state matches initially
});

You can test it out here. I assume in the above you want the #dates hidden if .always is checked, that's what it'll do.

like image 123
Nick Craver Avatar answered Sep 28 '22 09:09

Nick Craver


$('.always').change(function() {
 $('#dates').toggle(!this.checked);
});
like image 44
Aaron Hathaway Avatar answered Sep 28 '22 10:09

Aaron Hathaway