Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing date into a variable starting with a numeric 2

Status
Not open for further replies.

mikelawrence

Programmer
Joined
Sep 19, 2001
Messages
68
Location
GB
I want to get todays date and post it to another site that i do not have control off (salesforce.com). I think the problem is that the name of the field starts with a numeric and so javascript will not write to it.

My javascript date function is below:
var todaysdate = new Date();
var date = todaysdate.getDate();
var day = todaysdate.getDay() + 1;
var month = todaysdate.getMonth() + 1;
var yy = todaysdate.getYear();
var now=day+"/"+month+"/"+yy;
document.form2.00N20000000kUr52.value=now;

which gives me an error on the last line but if i change the last line to

document.form2.last_name2.value=now;

it appears to work, well at least it doesn't come back with an error. Am i right in this and if so is there a way around this? I'm also calling the function on a OnSubmit button, is there a better way of doing this so i can display the date on the page before I submit?

I'd appreciate any help,

mike

 

try:
Code:
document.getElementById('00N20000000kUr52').value = now;


A smile is worth a thousand kind words. So smile, it's easy! :-)
 
I dont think the id can start with a number.

But you should be able to access the form element by trawling through the document tree.

document.form2.element[1].value = now;

secondly:
if you use

<form onSubmit="return whatever()">

and

function whatever()
{
alert(now);
return true;
}

you will see the value before it is sent.

hope this helps

simon
 
Simon is right... IDs (and classes) must start with a letter:

From
# ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 

True Jeff, but as this is a third party site that the OP is trying to post to (and has no control over), there is unlikely to be a way for him to change it.

The getElementById method is a little more forgiving in Firefox, Opera and IE at least, so will work as requested - even with the non-standards-compliant naming convention developed by the other site (shame on you salesforce...;-)).

I probably should have explained in my first post rather than just quickly posting a workaround.

A smile is worth a thousand kind words. So smile, it's easy! :-)
 


you could try like I suggested:


for (var i = 0; i < document.form2.elements.length; i++)
{
if (document.form2.elements.id == '00N20000000kUr52')
{
document.form2.elements.value = now;
}
}

 

or even simpler: do it the way outlined in my original post.



A smile is worth a thousand kind words. So smile, it's easy! :-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top