javascripthtmlcssdrop-down-menuexpando

How to set expanded menu to be shut on page load


I have a div that expands on a click, but I can't work out how to set it to be un-expanded when the page is closed?

        <script>
     function toggleDiv(id1,id2) {
      var tag = document.getElementById(id1).style;
      var tagicon = document.getElementById(id2);

      if(tag.display == "none") {
       tag.display = "block";
       tagicon.innerHTML = "&nbsp;-&nbsp;";
      } else {
       tag.display = "none";
       tagicon.innerHTML = "&nbsp;+&nbsp;";
      }
     }
</script>

And the HTML:

<div class="title" onClick="javascript:toggleDiv('content1','icon1');">
     <span style="float:left">Options</span>
     <span id="icon1" style="float:right">&nbsp;-&nbsp;</span> 
    </div>
    <div id="content1" class="item">
     <div><input type="button" onclick="changeBG('#FF0097')" value="Magenta"/></div>
     <div><input type="button" onclick="changeBG('#A200FF ')"  value="Purple"/></div>
     <div><input type="button" onclick="changeBG('#00ABA9')"  value="Teal"/></div>
     <div><input type="button" onclick="changeBG('#8CBF26')"  value="Lime"/></div>
     <div><input type="button" onclick="changeBG('#F09609')" value="Orange"/></div>
     <div><input type="button" onclick="changeBG('#1BA1E2')"  value="Blue"/></div>
     <div><input type="button" onclick="changeBG('#E51400')"  value="Red"/></div>
     <div><input type="button" onclick="changeBG('#339933')"  value="Green"/></div>
    </div>

The CSS incase you need it:

.title {
      padding:5px; 
      border:1px solid #CCCCCC;
      font:15px arial; 
      width:100px; 
      cursor:pointer;
      height:20px;
     }

     .item {
      padding:5px; 
      border:1px solid #CCCCCC; 
      font:12px verdana; 
      width:100px;
     }

     .item div {
      border-bottom:1px solid #CCCCCC;
      padding:5px;
     }

     .button {
      border:1px solid #CCCCCC; 
      padding:5px;
      cursor: pointer;
     }

As you can see the drop down menu lets the user change the background color of the website. I was wondering if it would be possible to create a cookie to store the change so it doesn't default to white background first until the user selects a color?

Thank you Stack Overflow (:

All and any help is appreciated.


Solution

  • I am sure there is a body on your html page. just add a function like stated below. It works. I've tested

    <body onLoad="toggleDiv('content1','icon1')">