Codesnipp.it Social Code Sharing

Nate Hamilton

On checkbox status (onload) hide/show div ** PLEASE HELP**

by Nate Hamilton on Jun 24, 2011

<!-- I have the code working that says on click, make the div appear. But when you fill out the form and have a mistake, it refreshes the form. I have some php making the form sticky so the check box stays checked but the div doesn't stay open. I have a lot of these divs and checkbox's so getting the element by id doesn't really work quite well. Basically what I have set up is javascript pulling all the tags beginning with "toggle" and checking to see if they are selected. If they are selected it opens the div that begins with "opentoggle". Looking at the code might help more. Thanks! --> <script> $(document).ready(function(){ $('div[class^=opentoggle]').hide(); $('input[class^=toggle]').click(function() { var $this = $(this); var x = $this.attr("className"); if ($('.' + x).is(':checked')){ $('.open' + x).show(500); } else { $('.open' + x).hide(500); } }); }); </script> <div class="field-wrapper"> <h2>4. Select Campus(es)</h2> <ul class="checkboxes"> <li><input class="toggleephrata" type="checkbox" name="campus" value="Ephrata" <?php if (isset($_POST['campus'])) { if (($_POST['campus']) == 'Ephrata') { echo 'checked="checked"'; }} ?> >Ephrata</li> <li><input class="toggleharrisburg" type="checkbox" name="campus" value="Harrisburg" <?php if (isset($_POST['campus'])) { if (($_POST['campus']) == 'Harrisburg') { echo 'checked="checked"'; }} ?> />Harrisburg</li> <li><input class="togglelancaster" type="checkbox" name="campus" value="Lancaster City" <?php if (isset($_POST['campus'])) { if (($_POST['campus']) == 'Lancaster City') { echo 'checked="checked"'; }} ?> />Lancaster City</li> <li><input class="togglemanheim" type="checkbox" name="campus" value="Manheim" <?php if (isset($_POST['campus'])) { if (($_POST['campus']) == 'Manheim') { echo 'checked="checked"'; }} ?> />Manheim</li> <li><input class="toggleyork" type="checkbox" name="campus" value="York" <?php if (isset($_POST['campus'])) { if (($_POST['campus']) == 'York') { echo 'checked="checked"'; }} ?> />York</li> </ul> <div class="opentoggleephrata"><textarea name="ephratacampus" cols="70" rows="10" title="Ephrata - Please include all details that are specific to the Ephrata Campus"> <?php if (isset($_POST['ephratacampus'] )) echo $_POST['ephratacampus'] ?></textarea></div> <div class="opentoggleharrisburg"><textarea name="harrisburgcampus" cols="70" rows="10" title="Harrisburg - Please include all details that are specific to the Harrisburg Campus"> <?php if (isset($_POST['harrisburgcampus'] )) echo $_POST['harrisburgcampus'] ?></textarea></div> <div class="opentogglelancaster"><textarea name="lancastercampus" cols="70" rows="10" title="Lancaster City - Please include all details that are specific to the Lancaster City Campus"> <?php if (isset($_POST['lancastercampus'] )) echo $_POST['lancastercampus'] ?></textarea></div> <div class="opentogglemanheim"><textarea name="manheimcampus" cols="70" rows="10" title="Manheim - Please include all details that are specific to the Manheim Campus"> <?php if (isset($_POST['manheimcampus'] )) echo $_POST['manheimcampus'] ?></textarea></div> <div class="opentoggleyork"><textarea name="yorkcampus" cols="70" rows="10" title="York - Please include all details that are specific to the York Campus"> <?php if (isset($_POST['yorkcampus'] )) echo $_POST['yorkcampus'] ?></textarea></div> <div class="clear"></div> </div>

Can't see the comments? Please login first :)