Checkbox toggling with jQuery

<input id="mycheckbox" name="mycheckbox" type="checkbox" value="Y" />My Checkbox - click to toggle
<div id="mydiv" style="display:none;">This is a div that's to be shown/hidden.</div>
$('#mycheckbox').change(function(){ 
       $("#mydiv").slideToggle('slow');
  });
  
  if($('#mycheckbox').is(':checked')){ 
       $("#mydiv").slideToggle('slow');
  };
Here "mycheckbox" is the checkbox id and "mydiv" is the id of div that you want shown/hidden when toggling the checkbox.

See example below:

My Checkbox - click to toggle

Comments

  1. what if i want it reverse?
    show the div when uncheck?
    please help!

    ReplyDelete
    Replies
    1. @Nhoel: Then you'd just remove "display:none" from the CSS. This will initially show the content and selecting the checkbox will hide it. Hope this helps.

      Delete
  2. Exactly was I were looking for ... basic and simple jQuery! Thanks mate :-)

    ReplyDelete

Post a Comment

Popular posts from this blog

How to resolve 'res://ieframe.dll/acr_error.htm#' error (Internet Explorer stopped working while viewing a website)

htaccess: Redirecting multiple domains to the main domain

Removing index.php in Codeigniter using XAMPP on Windows