title: JavaScript - Show / Hide Div based on form selection (postback)
body: <p>I have a form which I am using to update an existing set of information in a database. Within it is a selection box, when option 'B' is chosen it shows a hidden  section, and on option 'A' it will hide the  section. This works fine. </p>
  
  <p>However, when I load a record set from the database and the selection box is set to option 'B' to show the hidden div, it is not shown. </p>
  
  <p>This is because I'm switching the DIV tag using 'onchange'. </p>
  
  <p>How do I get the JS to show the DIV tag correctly onload of the record set? Currently to see the data I have to toggle the selection box between the two options. </p>
  
  <p>I know next to nothing about JS, the JS below isn't mine, I'd appreciate some help.  </p>
  
  <p>Thanks</p>
  
  <pre><code>&lt;!--- show / hide DIV based on select ---&gt;
  
  &lt;script type="text/javascript"&gt;&lt;!--
  var lastDiv = "";
  function showDiv(divName) {
      // hide last div
      if (lastDiv) {
          document.getElementById(lastDiv).className = "hiddenDiv";
      }
      //if value of the box is not nothing and an object with that name exists, then change the class
      if (divName &amp;&amp; document.getElementById(divName)) {
          document.getElementById(divName).className = "visibleDiv";
          lastDiv = divName;
      }
  }
  //--&gt;
  &lt;/script&gt;
  &lt;style type="text/css" media="screen"&gt;&lt;!--
  .hiddenDiv {
      display: none;
      }
  .visibleDiv {
      display: block;
      border: 0px grey solid;
      }
  
  --&gt;&lt;/style&gt;
  &lt;!--- end DIV hide ---&gt;
  
  
  
  --------------snip----------
  
  
  
    &lt;select name="ad_i" id="ad_i" onchange="showDiv(this.value);"&gt;
        &lt;option value="in" &lt;? echo $adtypea; ?&gt; &gt;Option A &lt;/option&gt;
        &lt;option value="ba" &lt;? echo $adtypeb; ?&gt; &gt;Option B&lt;/option&gt;
     &lt;/select&gt;
  
     &lt;!--- start hiding DIV ---&gt;
  
    &lt;div class="hiddenDiv" id="ba"&gt;
  
    &lt;br /&gt;&lt;br /&gt;Coding for Ad&lt;br /&gt;
  
  &lt;select name="ad_type" id="ad_type"&gt;
        &lt;option value="html" &lt;? echo $codestylea; ?&gt; &gt;HTML&lt;/option&gt;
        &lt;option value="adsense"&lt;? echo $codestylef; ?&gt;&gt;Adsense / Other JavaScript Code&lt;/option&gt;
        &lt;option value="img" default="default" &lt;? echo $codestyleb; ?&gt;&gt;Image&lt;/option&gt;
        &lt;option value="swf" &lt;? echo $codestylec; ?&gt;&gt;Flash&lt;/option&gt;
     &lt;/select&gt;
  
  
     &lt;/div&gt;
     &lt;!--- end Div for hidden---&gt;
  </code></pre>