Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Add date to form field

  1. #1
    Join Date
    Sep 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Add date to form field

    Hi all, I'm trying to add the date after the username in a formfield on my shoutbox, it allready works with adding smilies into a message, but a can't get it done with the date, script looks like this:

    Code:
    function addDate(date, smilieForm, nameField) {
    var day = new Date(); 
    // following all on one line 
    //Months is zero-based (Jan=0, Feb=1, Mar=2, Apr=3, Mei=4, Jun=5, Jul=6, Aug=7, Sep=8, Okt=9, Nov=10, Dec=11) So must be incremented 
    var submitted = (+day.getHours()+':'+day.getMinutes());
            var revisedNaam;
            var currentNaam = document.smilieForm.elements[NaamField].value;
            revisedNaam = currentNaam+Date;
            document.smilieForm.elements[NaamField].value=revisedNaam;
            document.smilieForm.elements[NaamField].focus();
            return;
    }
    For the button there's this code:
    <input type="button" name="datum" onclick="addDate('date','smilieform','name')" value="Datum" class="button" />

    Can someone tell me wat is going wrong, I'm a newbee on javascript but this is wat i've hoped to work.

    Tanks in advance

  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

    Just looking at the code, this:

    revisedNaam = currentNaam+Date;

    should be:

    revisedNaam = currentNaam+day;

    You might want to parse out the date though to remove the time:

    Code:
    function addDate(date, smilieForm, nameField) {
    var day = new Date(); 
    // following all on one line 
    //Months is zero-based (Jan=0, Feb=1, Mar=2, Apr=3, Mei=4, Jun=5, Jul=6, Aug=7, Sep=8, Okt=9, Nov=10, Dec=11) So must be incremented 
    var submitted = (+day.getHours()+':'+day.getMinutes());
    var mo = day.getMonth()+1
    var dt = day.getDate()
    var yr = day.getFullYear()
    day = mo+"/"+dt+"/"+yr
            var revisedNaam;
            var currentNaam = document.smilieForm.elements[NaamField].value;
            revisedNaam = currentNaam+day;
            document.smilieForm.elements[NaamField].value=revisedNaam;
            document.smilieForm.elements[NaamField].focus();
            return;
    }
    - John
    ________________________

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

  3. #3
    Join Date
    Sep 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    instead of using document.smilie.whatever, use the W3C standard of using "getElementByID" or "getElementByName" as needed.
    more info: http://www.w3schools.com/htmldom/dom_obj_document.asp

    And in this case...
    HTML Code:
    <input type="button" name="datum" onclick="addDate('date','smilieform','name')" value="Datum" class="button" />
    if u do "getElementByID", then u'll need to change that code above to:
    HTML Code:
    <input type="button" [B]id[/B]="datum" onclick="addDate('date',[B]'datum'[/B],'name')" value="Datum" class="button" />
    (with this method, you don't even need the third parameter for the function).

    O...one thing I noticed, you have nameField misspelled as "NaamField".

    here's my modification for the getElementByID version (keep in mind that I didn't test it at all yet... ):
    Code:
    function addDate(date, formElementID) {
    var day = new Date(); 
    // following all on one line 
    //Months is zero-based (Jan=0, Feb=1, Mar=2, Apr=3, Mei=4, Jun=5, Jul=6, Aug=7, Sep=8, Okt=9, Nov=10, Dec=11) So must be incremented 
    var submitted = (+day.getHours()+':'+day.getMinutes());
            var revisedNaam;
            var formElement = getElementByID(formElementID);
            var currentNaam = formElement.value;
            revisedNaam = currentNaam+day;
            formElement.value=revisedNaam;
            formElement.focus();
            return true;
    }
    Last edited by MoFoQ; 09-07-2005 at 04:37 PM. Reason: typo

  4. #4
    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

    Using the document collection is valid and preferred in this case as it is with all forms and images.
    - John
    ________________________

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

  5. #5
    Join Date
    Sep 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    but not W3C.

  6. #6
    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 think it is by w3c, please site your reference exactly.
    - John
    ________________________

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

  7. #7
    Join Date
    Sep 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

  8. #8
    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

    That's about as exact as your code, actually much less so. I know where their website is, where on it does it state that getElementById is to be used instead of the document collection for forms, images and frames?
    - John
    ________________________

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

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

    Default

    HTML Code:
    <input type="button" [B]id[/B]="datum" onclick="addDate('date',[B]'datum'[/B],'name')" value="Datum" class="button" />
    Can't put BB tags inside [html]
    O...one thing I noticed, u have nameField misspelled as "NaamField".
    That's not necessarily a misspelling. How do you know s/he speaks English (see "Mei," "Okt")? Variable naming is entirely at the discretion of the developer.
    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!

  10. #10
    Join Date
    Sep 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Yess, this works fine, there were a fiew little things i had to change but this does the job, here's the code:
    <CODE>function addDate(date, smilieForm, nameField) {
    var day = new Date();
    // following all on one line
    //Months is zero-based (Jan=0, Feb=1, Mar=2, Apr=3, Mei=4, Jun=5, Jul=6, Aug=7, Sep=8, Okt=9, Nov=10, Dec=11) So must be incremented
    var submitted = (+day.getHours()+':'+day.getMinutes());
    var mo = day.getMonth()+1
    var dt = day.getDate()
    var yr = day.getFullYear()
    day = mo+"/"+dt+"/"+yr
    var revisedNaam;
    var currentNaam = document.smilieForm.elements[nameField].value;
    revisedNaam = currentNaam+day;
    document.smilieForm.elements[nameField].value=revisedNaam;
    document.smilieForm.elements[nameField].focus();
    return;
    }
    </CODE>

    And the button should be like this:
    <code><input type="button" name="date" onclick="addDate('day','smilieForm','Naam')" value="Datum" /></code>

    Now there's just one thing, how do I put a small whitespace or ~ between the username and the date?

    Thanks for your help.

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
  •