How do you imput date values into hidden fields

Discussion in 'JavaScript' started by Imozeb, Feb 5, 2010.

  1. #1
    How do you imput date values into hidden fields? I checked my code and this should work?

    I have this for my javascript:

    //get date
    var currentTime = new Date()
    //format it
    var month = currentTime.getMonth() + 1
    var day = currentTime.getDate()
    var year = currentTime.getFullYear()
    var datej = month + "/" + day + "/" + year)
    //change hidden field
    document.getElementById("date").value = datej;

    and my form's hidden field's ID = "date"


    Thanks in advance!

    ~Imozeb
     
    Imozeb, Feb 5, 2010 IP
  2. mariush1007

    mariush1007 Member

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    hi,
    Try to wrap Your code into window.onload event (maybe this is a problem - maybe Your javascript code is trying to access html element - element with date id here - before it is even loaded) and of course replace round bracket with semicolon at the end of this line:

    var datej = month + "/" + day + "/" + year;
    Code (markup):
    so finally try below code:

    window.onload=function() {
      //get date
      var currentTime = new Date();
      //format it
      var month = currentTime.getMonth() + 1;
      var day = currentTime.getDate();
      var year = currentTime.getFullYear();
      var datej = month + "/" + day + "/" + year;
      //change hidden field
      document.getElementById("date").value = datej;
    }
    Code (markup):
     
    mariush1007, Feb 6, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    No, that's not the problem because the only way to call the script is from the submit button in the form and that only loads once the entire page has loaded and the user has imputed all the values. Any other ideas?
     
    Imozeb, Feb 6, 2010 IP
  4. mariush1007

    mariush1007 Member

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #4
    hmm... I was trying above piece of code on my computer and it works without any problems, so please send a link to Your website because there is probably problem somewhere else on Your website.
     
    mariush1007, Feb 6, 2010 IP
  5. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I would send a link to my website but I've already deleted the code because I was having a problem with some PHP validation. I have to work on the PHP part before I try this code again. Sorry.
     
    Imozeb, Feb 7, 2010 IP