PDA

View Full Version : Add date to form field



Woekaah
09-06-2005, 08:42 PM
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:


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

jscheuer1
09-07-2005, 04:33 PM
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:


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;
}

MoFoQ
09-07-2005, 04:36 PM
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...


<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:


<input type="button" id="datum" onclick="addDate('date','datum','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". :p

here's my modification for the getElementByID version (keep in mind that I didn't test it at all yet... :p ):


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;
}

jscheuer1
09-07-2005, 04:38 PM
Using the document collection is valid and preferred in this case as it is with all forms and images.

MoFoQ
09-07-2005, 04:39 PM
but not W3C. ;)

jscheuer1
09-07-2005, 04:40 PM
I think it is by w3c, please site your reference exactly. :p

MoFoQ
09-07-2005, 04:44 PM
ummm.... http://www.w3.org :D

jscheuer1
09-07-2005, 05:07 PM
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?

Twey
09-07-2005, 05:22 PM
<input type="button" id="datum" onclick="addDate('date','datum','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.

Woekaah
09-07-2005, 05:45 PM
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.

Woekaah
09-07-2005, 05:56 PM
this is Dutch, I'm a Dutchman from The Netherlands so that's wy it's spelled like this, but for the script to work right "NaamField" was spelled wrong though ;)

Twey
09-07-2005, 06:01 PM
revisedNaam = currentNaam + " " + day;

mwinter
09-11-2005, 07:47 PM
Using the document collection is valid and preferred in this case as it is with all forms and images.The document object is not a collection, and using it as such does not meet the API as recommended by the W3C. This shortcut mechanism was introduced by browser vendors, however it is my opinion that this was a mistake as it can cause clashes with other properties of both the document object, and form objects.

This doesn't mean that MoFoQ is correct. In fact I would recommend against the use of the getElementById and getElementsByName methods when working with forms, form controls, or images. Instead, use the forms, elements, and images collections, respectively. Each of these can use numeric indices, or strings that match name or id attributes. This is supported by more browsers than any other approach, though some obsolete browsers (NN4) will not recognise id attributes.

There is only one case where, in principle, the elements collection should not be used to retrieve object references to form controls, and that is where one has multiple form controls that share the same control name. The W3C API specifies that the elements collection retrieves a single object reference, whereas the de facto behaviour will return all elements that match that name or id value as another collection when there is more than one match. In this case, the getElementsByName property should be used instead.

However, that's in principle. This is one of the few occasions where I think de facto behaviour is acceptable.

Mike

kitten2781
10-14-2008, 09:45 PM
This code isn't working for me :confused:



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.