Web Analytics Made Easy -
StatCounter How Can I Pass a variable to HTML code - CodingForum

Announcement

Collapse
No announcement yet.

How Can I Pass a variable to HTML code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How Can I Pass a variable to HTML code

    I have an HTML page for users to view schedules divided by montly links - Jan thru December in a column. I don't want March - December Links to be "Visible" in February. I use JS to detemine the current Month and want to pass that variable into a div -as below. I need to pull the div variable with double quotes to use in the <div ID="div2" .... If I can get the variable passed with quotes I can struggle thru the test fors. Any suggestions ? Am I in left field trying to pass variable or is the div variable confusing my script or do I just need to get the double quotes in place in the ID= section ? Thanks in advance - I'm lost. PatK

    <style type="text/css">
    #div1{
    position: relative;
    visibility: visible;
    }
    #div2{
    position: relative;
    visibility: hidden;
    }</style>

    <script >
    var div;
    div = 0;
    document.write(div);
    datetoday = new Date();
    timenow=datetoday.getMonth() + 1;
    if (timenow > 0) div = "div"+timenow;
    else div = "divfalse"+timenow;//not relevant
    document.write(div);
    // End -->
    </script>


    <div ID = Variable div in quotes align="center<a href="file:///L|/Tank%20Truck%20Schedule/Tank%20Truck%20Schedule%200304.xls">March
    2004 </a></div>

  • #2
    Hi Pat;

    Try this:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    <!--//
     var month = new Date().getMonth()+1;
      function displayMonth(num){
        for(var i=1; i<=12; i++){
         var which = document.getElementById;
          if(num == i || num-1 == i){
             which('div'+i).style.display = 'block';
          }
             else{
                  which('div'+i).style.display = 'none';
             }
        }
       return false;
      }
    //-->
    </script>
    </HEAD>
    
    <BODY onload="displayMonth(month)">
    <div id="div1" style="display:none;"><a href="#">Jan</a></div>
    <div id="div2" style="display:none;"><a href="#">Feb</a></div>
    <div id="div3" style="display:none;"><a href="#">Mar</a></div>
    <div id="div4" style="display:none;"><a href="#">Apr</a></div>
    <div id="div5" style="display:none;"><a href="#">May</a></div>
    <div id="div6" style="display:none;"><a href="#">Jun</a></div>
    <div id="div7" style="display:none;"><a href="#">Jul</a></div>
    <div id="div8" style="display:none;"><a href="#">Aug</a></div>
    <div id="div9" style="display:none;"><a href="#">Sep</a></div>
    <div id="div10" style="display:none;"><a href="#">Oct</a></div>
    <div id="div11" style="display:none;"><a href="#">Nov</a></div>
    <div id="div12" style="display:none;"><a href="#">Dec</a></div>
    
    </body>
    </html>
    .....Willy

    Comment


    • #3
      I think it's better to make all the links visible by default so that even if javascript is disabled, the page is still usable (all the links are present).
      Glenn
      vBulletin Mods That Rock!

      Comment

      Working...
      X