Results 1 to 3 of 3

Thread: putting javascript in a disabled text box in php..

  1. #1
    Join Date
    Aug 2006
    Posts
    65
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default putting javascript in a disabled text box in php..

    I cant seem to figure out how to put this javascript code into the disabled text box... the javascript code automatically fills in todays date..

    here is the disabled text box
    Code:
    <tr><td width="73">Event Date:</td>
    <td width="715"><input name=date type=text disabled value="Todays Date">
    </td></tr>

    and here is the javascript code
    Code:
    <SCRIPT Language="JavaScript">
    <!-- hide from old browsers
      var today = new Date()
      var month = today.getMonth()+1
      var year = today.getYear()
      var day = today.getDate()
      if(day<10) day = "0" + day
      if(month<10) month= "0" + month 
      if(year<1000) year+=1900
         
      document.write(month + "/" + day +
                     "/" + year) 
    //-->
    </SCRIPT>
    can someone show me how to put in it the disabled text box

  2. #2
    Join Date
    Sep 2006
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    You are writing to the page, not to the form element.
    So instead of document.write, use document.forms

    var today = new Date()
    var month = today.getMonth()+1
    var year = today.getYear()
    var day = today.getDate()
    if(day<10) day = "0" + day
    if(month<10) month= "0" + month
    if(year<1000) year+=1900

    yourDate = month+'/'+year
    document.forms[0].date.value = yourDate;

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <script type="text/javascript">
    Number.prototype.pad = function(n) {
      var l = this.toString();
      while(l.length < n)
        l = "0" + l;
      return l;
    };
    
    window.onload = function() {
      var c;
      document.forms[0].elements['date'].value = ((c = new Date()).getMonth() + 1).pad(2) + "/" + c.getFullYear().pad(2);
    };
    </script>
    Just output that anywhere on the page. Note that it assumes your form is the first one on the page; if it isn't, change the number or give your form a name and reference it by that.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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