Image

Using checkboxes to show/hide fields in a form

I'm relatively new to Javascript and PHP, so if anyone can help me with this I'd really appreciate it.

I have a nice bit of code which allows me to hide fields, but no matter how I play around with it I can't quite figure out how to make hiding them the default option so that I can click to show them instead. The code is as follows:

<script language="JavaScript" type="text/javascript">
  function showhidefield()
  {
    if (document.cv.chkbox.checked)
    {
      document.getElementById("hideablearea").style.display = "block";
    }
    else
    {
      document.getElementById("hideablearea").style.display = "none";
    }
}
</script>


with the following where I want to hide the boxes:

<input onclick="showhidefield()" type="checkbox" name="chkbox" value="on" /> Instructions 
<div id="hideablearea">
STUFF TO HIDE 
</div>


Does anyone have any thoughts on what I could do with this? I've tried altering the script by swapping the two style display lines, with the box automatically checked, but that didn't work - just meant that I had to click twice before it hid the rest of it. The same happened when I tried changing the script to read checked == false instead. Any other suggestions would be great, either for this script or a different way of doing it.

Thanks in advance.