views:

85

answers:

3

This script I have found on StackOverflowe only...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
  <style>
    #appear_div { display: none; }
  </style>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
  <script>
    $(document).ready(function() {
      $('#appear').click(function() { $('#appear_div').show(); });
    });
  </script>
</head>
<body>
  <input type="checkbox" id="appear">
  <div id="appear_div">
  <input type="checkbox" id="cb1">Check me <input type="text" id="text1">
  </div>
</body>
</html>

I need further development with this.

I want to hide div when uncheck the checkbox.

& How to add Fading effect to it ?

Thanx

+2  A: 

Assuming your talking about the checkbox labeled with id=cb1

$(document).ready(function() { 
  $("#cb1").click(function() { 
    if (this.checked) {
      $('#appear_div').fadeIn('slow'); 
    }
    else {
      $('#appear_div').fadeOut('slow'); 
    }
  }); 
}); 

if your talking about the other checkbox labeled with id=appear then use this

$(document).ready(function() { 
  $("#appear").click(function() { 
    if (this.checked) {
      $('#appear_div').fadeIn('slow'); 
    }
    else {
      $('#appear_div').fadeOut('slow'); 
    }
  }); 
}); 
John Hartsock
`$(this).checked` should be just `this.checked`, it's not a jQuery property, but a direct DOM one.
Nick Craver
good catch... Thanks Nick
John Hartsock
+3  A: 
$('#appear').click(function() {
    if(this.checked) {
        $('#appear_div').fadeIn();
    } else {
        $('#appear_div').fadeOut();
    }
});
I.devries
A: 

You can do the fading toggle like this:

$(function() {
  $('#appear').change(function() { 
    $('#appear_div').animate({opacity: this.checked ? 1 : 0}); 
  }).change();
});​

You can see a demo here. If it's .checked, we .animate() the opacity to 1 (faded in), otherwise we change it to 0 (faded out), the last .change() call is to set the state properly when the page first loads. If you want to change the speed, to say 2 seconds, just add it as the next option:

$('#appear_div').animate({opacity: this.checked ? 1 : 0}, 2000);
Nick Craver
Thanks Nick for the demo.
MANnDAaR