Brent Eamer wrote on 18 Dec 2003 at Thu, 18 Dec 2003 13:11:36 GMT:
function SetDefaultDate() {
d = new Date();
return d;
}
.......
<TD align=left> Start Date: </TD>
<TD align=left> <SELECT name="batchStartDate" size="1"
maxlength="50" value="<SCRIPT>SetDefaultDate()</SCRIPT>">
</TD>
</TR>
I was going to say: "That SCRIPT block is being interpreted as it's
supposed to be in that context: a CDATA (string) value." But when I
checked the HTML standard (to check that value attributes contain
CDATA), I realised that SELECT elements don't have value attributes;
the selected OPTION elements are the values for the SELECT. SELECT
elements don't have a maxlength attribute, either. Basically, even
if the script part of your HTML syntax was correct, which it isn't,
the above still wouldn't work.
Without knowing exactly what you were trying to acheive, I can't
give a definitive answer, but I can point you in the right
direction.
As I already said, what you are expecting from that HTML is *never*
going to happen. It's like specifying an IMG element as the value of
a BUTTON and expecting it to render the image:
<BUTTON value="<IMG src='someImage.gif'>"></BUTTON> [1]
There are two methods of doing what you hope to acheive:
1) Write the entire element using document.write(), concatenating
the date into a fixed string, or;
2) Assign the value to the element at some point after creating it
(when the onload event is fired, for example).
I'll assume below that in your HTML you wanted a drop-down box that
displayed, as it's initial option, the current local date only (not
time).
An example for the first method:
<SELECT name="batchStartDate" size="1">
<SCRIPT type="text/javascript">
// Notice that you forgot to specify the script type in your
// example.
var now = new Date();
document.write(
'<OPTION selected>' + now.getFullYear() + '-'
+ now.getMonth() + '-' + now.getDate() + '<\/OPTION>' );
// Produces the HTML: <OPTION selected>YYYY-M-d</OPTION>
//
// where YYYY is a four letter year
// M is the month (no leading zero)
// d is the day (no leading zero)
</SCRIPT>
</SELECT>
An example for the second method:
<HEAD>
<!-- Set the default script language
for the onload event below -->
<META http-equiv="Content-Script-Type"
content="text/javascript">
<SCRIPT type="text/javascript">
function insertCurrentDate() {
var now = new Date();
var value = now.getFullYear() + '-' + now.getMonth() + '-'
+ now.getDate();
var initial = null;
// Find the OPTION element and replace it's display text and
// value to the current date
if (document.getElementById) {
var initial = document.getElementById('initial');
} else if (document.all) {
var initial = document.all['initial'];
}
if (initial) {
initial.text = value;
initial.value = value;
}
}
</SCRIPT>
...
</HEAD>
<BODY onload="insertCurrentDate()">
...
<SELECT name="batchStartDate" size="1">
<OPTION id="initial" selected>Today</OPTION>
...
</SELECT>
I *seriously* recommend that you refresh your knowledge of HTML and
JavaScript.
Mike
[1] The correct syntax is, of course:
<BUTTON><IMG src="someImage.gif"></BUTTON>
--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk")