Hello and welcome to our community! Is this your first visit?
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
    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.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <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)
    var theday=mydate.getDay()
    var themonth=mydate.getMonth()+1
    if (themonth<10)
    var theday=mydate.getDate()
    if (theday<10)
    //////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;
    <p align="left"><strong>Example:</strong></p>
    <form name="dform" method="post">
      <table border="0" cellspacing="0" cellpadding="2">
          <td><strong>Enter today's date: </strong></td>
          <td><input type="text" name="currentdate" size="11"></td>
          <td><strong>Your name:</strong></td>
          <td><input type="text" name="T1" size="30"></td>
          <td><div align="right"><p><input type="submit" value="Submit!" name="B1"></td>
          <td><input type="reset" value="Reset" name="B2"></td>

  • #2
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Camarillo, CA US
    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.
    window.onload= function(evt){ document.dform.currentdate= date; }
    (using a window.onload event handler)


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

    <!-- your form and whatever else is in the body of your document //-->
    document.dform.currentdate= date;
    (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