Results 1 to 6 of 6

Thread: Javascript getDate() Problem

  1. #1
    Join Date
    Jan 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Javascript getDate() Problem

    Hello,
    I've got this script that doesn't quite work. It is supposed to get todays date and populate 2 (actually 3 - I am not using the yearfield) pulldowns with that date as well as the options to change the date to any other month and day of the month. It is working except that it doesn't populate a option for the day of the month following "today's date". It just has todays date listed 2 times in the pulldown. Any ideas? Thanks!

    Here is the page:
    http://www.crosst.org

    CODE:
    Code:
    var monthtext=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];
    
    function populatedropdown(monthfield, dayfield, yearfield){
    var today=new Date()
    var dayfield=document.getElementById(dayfield)
    var monthfield=document.getElementById(monthfield)
    var yearfield=document.getElementById(yearfield)
    for (var i=0; i<31; i++)
    dayfield.options[i]=new Option(i+1, i+1)
    dayfield.options[today.getDate()]=new Option(today.getDate(), today.getDate(), true, true) //select today's day
    for (var m=0; m<12; m++)
    monthfield.options[m]=new Option(monthtext[m], monthtext[m])
    monthfield.options[today.getMonth()]=new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true) //select today's month
    var thisyear=today.getFullYear()
    for (var y=0; y<20; y++){
    yearfield.options[y]=new Option(thisyear, thisyear)
    thisyear+=1
    }
    yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true) //select today's year
    }
    Last edited by jscheuer1; 05-22-2008 at 03:10 PM. Reason: add code tags to code block

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If there is no:

    Code:
    var yearfield=document.getElementById(yearfield)
    There will be an error and is. But FF and IE 7 recover from it. And it looks fine in those browsers (notwithstanding the alert flag bottom left in IE 7). What browser are you using?

    You could always make a yearfield select element with its display property set to none. That would get rid of the error, and perhaps take care of the other problem.
    Last edited by jscheuer1; 05-22-2008 at 03:18 PM. Reason: respond more appropriately
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jan 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I am testing on all of them. I was aware of the script error for yearfield, it was still showing todays date 2 times in the populated pulldown. I altered the code :

    for (var i=0; i<31; i++)
    dayfield.options[i]=new Option(i+1, i+1)

    To:
    for (var i=1; i<32; i++)
    dayfield.options[i]=new Option(i, i)

    Now it works fine, don't know exactly why though Thanks!

  4. #4
    Join Date
    Jan 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    One more snafu with the code. I've fixed the earlier problem that displayed todays date twice when it populated the dropdowns, but now there are allways an additional day in every populated dropdown for the days of the month. Any suggestions?

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm not seeing that.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jan 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    The code is fixed now. Sorry I forgot to post when I changed it. Thanks for looking at it though.

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
  •