Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Date not filled into a form element

    I would like to put the date into a form so the user does not have to type it. I got this javascript from www.javascript.com. I made a modification from the origonal (made a var out of 'date' to do a document.write) which works fine but the date does not show up in the form example that is included. This does not work in Safari or FireFox. Thanks for any help.
    Norm

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title>test html for date form fill</title>
    <!--<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> -->
    <script language="javascript" type="text/javascript">
    
    /*Current date in form credit: 
    JavaScript Kit (www.javascriptkit.com)
    Over 200+ free scripts here!
    */
    
    var mydate=new Date()
    var theyear=mydate.getYear()
    if (theyear < 1000)
    theyear+=1900
    
    var theday=mydate.getDay()
    var themonth=mydate.getMonth()+1
    if (themonth<10)
    themonth="0"+themonth
    
    var theday=mydate.getDate()
    if (theday<10)
    theday="0"+theday
    
    
    //////EDIT below three variable to customize the format of the date/////
    
    var displayfirst=themonth;
    var displaysecond=theday;
    var displaythird=theyear;
    
    ////////////////////////////////////
    var date = displayfirst+"/"+displaysecond+"/"+displaythird;
    //document.write(date);
    document.dform.currentdate.value=date;
    </script>
    </head>
    <body>
    <p align="left"><strong>Example:</strong></p>
    <form name="dform" method="post">
      <table border="0" cellspacing="0" cellpadding="2">
        <tr>
          <td><strong>Enter today's date: </strong></td>
          <td><input type="text" name="currentdate" size="11"></td>
        </tr>
        <tr>
          <td><strong>Your name:</strong></td>
          <td><input type="text" name="T1" size="30"></td>
        </tr>
        <tr>
          <td><div align="right"><p><input type="submit" value="Submit!" name="B1"></td>
          <td><input type="reset" value="Reset" name="B2"></td>
        </tr>
      </table>
    </form>
    </body>
    </html>

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Ok, here's the problem. The SCRIPT tag is in the document's HEAD. So at that point in time, it doesn't know anything about the document.dform.currentdate element. What you need to do is have it assign the date value after the document body has loaded. This can be accomplished in several ways, e.g.
    Code:
    window.onload= function(evt){ document.dform.currentdate= date; }
    (using a window.onload event handler)

    -or-

    Code:
    <body onload="setDate()">
    ( a BODY tag inline handler, where setDate is a function that sets the date value in the form element)

    -or-
    Code:
    <body>
    <!-- your form and whatever else is in the body of your document //-->
    <script>
    document.dform.currentdate= date;
    </script>
    </body>
    </html>
    (putting your SCRIPT tag and any code you want executed after the document body has loaded down just before the closing BODY tag)

    I take the third option one step further and pushing all my JavaScript code to the bottom of the document body.

    Note that in all three examples above, date is globally accessible because of how you assign it in the script, so these work as-is.
    Last edited by bdl; 04-24-2009 at 03:10 AM.

  • Users who have thanked bdl for this post:

    normc19 (04-28-2009)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •