SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2001
    Location
    San Francisco
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    concatenating javascript string

    hello - I am trying to simplify a date picking function for a calendar application I am building. I have a form that gets submitted to a database and previously I was making users type the date of the program they were entering using a format that the database could use (YYYY-MM-DD).

    I thought I could make things easier on them, if not me, by using pulldowns for the date elements. The idea would be that they could pick the year, the month, and the day off the pulldowns and I'd have some javascript that would bring it together and format the date in YYY-MM-DD format.

    So, what I have come up with is as follows. When they select the day, it calls the checkdate() function. In this function:

    function checkdate()
    {
    var year_val = window.document.content_ui.year.selectedIndex;
    var year_select = window.document.content_ui.year.options[year_val].value;
    var month_val = window.document.content_ui.month.selectedIndex;
    var month_select = window.document.content_ui.month.options[month_val].value;
    var day_val = window.document.content_ui.day.selectedIndex;
    var day_select = window.document.content_ui.day.options[day_val].value;
    window.document.content_ui.scheduled.value = year_select ?this is where I'm having problems? ;
    }

    I can get it so it writes the year in there, but even then, that only works when they first make selections of the other two. This is not so much a problem, as the larger problem of being able to make window.document.content_ui.scheduled.value = year_select - month_select - day_select;

    I just want to make the 'scheduled' input box have something like 2001-12-29. So I have to somehow string the values of year_select month_select and day_select together with hyphens in between.

    Anyone know how to do this? General susggestions on how to improve the code also very, very welcome. Thanks.

    Ian
    signature

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    window.document.content_ui.scheduled.value =
       year_select + '-' + month_select + '-' + day_select;
    Should work. Similar to the Java language the '+' operator is the string concatenation operator too.


  3. #3
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi nobodaddy

    function....
    {
    var yr = document.content_ui.year.options[document.content_ui.year.selectedIndex].value;

    var mnt = document.content_ui.month.options[document.content_ui.month.selectedIndex].value;

    var day = document.content_ui.day.options[document.content_ui.day.selectedIndex].value;

    document.content_ui.scheduled.value =
    yr + '-' + mnt + '-' + day
    }

    you only need to specify a window if you are dealing with more than one frame or a popup. You do not need to specify the selectedIndex as a separate var; if you feel you must, re-use the var; don't create one for each selection list.

    finally you should 'lock' the text box in the following manner:

    <input type='text' name='scheduled' onFocus='this.blur()'>

    that will prevent the user from changing the value once you fill it with your function.

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still


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
  •