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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getElementByID and hidden field

Status
Not open for further replies.

GiddyRob

Programmer
Aug 25, 2005
37
GB
Hey,

can someone please tell me why the code below will not add a value to a hidden field, but it will output to screen:

<SCRIPT LANGUAGE="Javascript">
<!--

// Array of day names
var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday");

// Array of month Names
var monthNames = new Array(
"January","February","March","April","May","June","July",
"August","September","October","November","December");

var now = new Date();
var super = (dayNames[now.getDay()] + ", " +
monthNames[now.getMonth()] + " " +
now.getDate() + ", " + now.getFullYear());
// -->
</script>

<body><script>document.write(dayNames[now.getDay()] + ", " +
monthNames[now.getMonth()] + " " +
now.getDate() + ", " + now.getFullYear());
document.getElementByID("newsdates").value = (super);</script>
<input type="hidden" name="newsdates" id="newsdates" value="" />
</body>
</html>

When I load the page it should add the date to the value by id, but I cant get it to work.

Thanks in advance

Cheers

Rob
 
2 things:

1. You are trying to add the value, before the input, which means as the page loads the JS is executed first, before the actual input is created. So it can't actually address the input yet to change its value.
If you need to run JS when the page loads, use the onLoad event of the body tag. That assures that everything in the page has loaded and is accessible.

2. Most importantly, because Javascript is case sensitive. so getElementByI[red]D[/red] does not exist.

Try getElmentByI[blue]d[/blue] instead.

Both of these errors would have been easily identifiable if you use the browser's error display. Both IE and FF have ways of looking at JS errors.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
thanks for the reply :)

I have now change the code to this:

<body>
<input type="hidden" name="newsdate" id="newsdates" value="" />
<SCRIPT LANGUAGE="Javascript">
<!--

// Array of day names
var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday");

// Array of month Names
var monthNames = new Array(
"January","February","March","April","May","June","July",
"August","September","October","November","December");

var now = new Date();
var super = "yes";
document.getElmentById("newsdates").value = (yes);
// -->
</script>
</body>
</html>

It still doesn't work. I get a "has not method" error on getElementById

Cheers

Rob
 
You are missing an E:
Code:
document.getEl[red]e[/red]mentById("newsdates").value = (yes);



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
<body>
<input type="hidden" name="newsdate" id="newsdates" value="" />
<SCRIPT LANGUAGE="Javascript">
<!--

// Array of day names
var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday");

// Array of month Names
var monthNames = new Array(
"January","February","March","April","May","June","July",
"August","September","October","November","December");

var now = new Date();
var super = "yes";
document.getElementById("newsdates").value = (super);
// -->
</script>
</body>
</html>

Still not working :( I really appreciate your help :)
 
Your last piece of code is working for me.

The hidden input gets the "Yes" value.

If you are expecting the actual source code to change, it won't.

But if you submit the form, or attempt to access the value for the input after setting it you'll see its value is "Yes".



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Oh! I thought it would show up in the source code :(

doh!

thanks for your help though. I understand more how this works now :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top