What the he** am I missing here?

B

Brent Eamer

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 just want to default the html control to todays date.

Why is this all so painful......maybe time for a career change :(
 
@

@SM

Brent Eamer a ecrit :
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>">

may be ... ?
 
M

Michael Winter

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

kaeli

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 just want to default the html control to todays date.

Why is this all so painful......maybe time for a career change :(

Because you're trying to set a value to a select element when select
elements have options and selectedIndex, not values. You're also giving
it a maxlength when that is not a property of select elements. Select
elements have options. Options have values.
Choose an option with your script (you'll either need a loop or you'll
need to know the option index to set it), then call the script in the
onLoad of the body. Note that this will muck things up if the user
presses the back button after leaving the page, as the date will be
reset instead of holding on as the user expects. To help this, I check
for a value before resetting it in my stuff.
If this were a text element, you should still set in the script by using
document.formname.elementname.value=d;
not try to call a script like you did above inside the value quotes.
That works fine with server-side <%= val %> type statements, but I've
never seen it used client-side with a script block.

--
 
M

McKirahan

Brent Eamer said:
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 just want to default the html control to todays date.

Why is this all so painful......maybe time for a career change :(

1) size="1" does nothing useful

2) maxlength="50" does nothing

3) SELECT does not support "value="

4) You cannot assign a form value with JavScript in that manner.

5) Are you sure you want a SELECT?



Perhpas you want; (watch for word-wrap):

<script language="javascript" type="text/javascript">
<!--
var html = "<input type='text' name='BatchStartDate' size='10'
maxlength='10' value='";
var when = new Date();
html += when.getMonth()+1 + "/";
html += when.getDate() + "/";
html += when.getFullYear() + "'>";
document.write(html);
// -->
</script>


Note that the month and day will have leading zeroes suppressed; thus New
Year's Day will be shown as "1/1/2004".
 
B

Brent Eamer

Sorry guys, you are right, I don't want a select, cut and paste error. I
wanted the following:

function SetDefaultDate() {
rightnow = new Date("Month dd, yyyy");
return rightnow;
}

<TD align=left> <input type="text" name="batchStartDate" size="20"
maxlength="50" value="SetDefaultDate()">


However it still does not work, sorry for the confusion
 
M

McKirahan

Sorry to be picky but...

"now.getMonth()" returns 0 to 11 (not 1 to 12).

You example will display the previous month.


Michael Winter said:
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>
 
B

Brent Eamer

Refresh?
I'm fairly new to this stuff :)

I'm an old Oracle PL/SQL programmer, and boy some of this stuff can really
trip you up. Trying to upgrade my skills during my unemployment

Thanks

Michael Winter said:
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>
 
M

Michael Winter

@SM wrote on 18 Dec 2003 at Thu, 18 Dec 2003 14:57:22 GMT:
may be ... ?

value="<SCRIPT>document.write(SetDefaultDate())</SCRIPT>">

Umm, no. I think you need to review your knowledge of HTML, too.

See my other post.

Mike
 
M

McKirahan

The variable "html" is be "constructed"; the "document.write()" statement
will display:

<input type='text' name='BatchStartDate' size='10' maxlength='10'
value='12/18/2003'>";


Just cut-and-paste the code into a Web page and try it -- it works as-is.
 
M

McKirahan

Sorry, "is be" should have been "is being".


McKirahan said:
The variable "html" is be "constructed"; the "document.write()" statement
will display:

<input type='text' name='BatchStartDate' size='10' maxlength='10'
value='12/18/2003'>";


Just cut-and-paste the code into a Web page and try it -- it works as-is.
 
D

Douglas Crockford

All JavaScript lines should end in semicolons.
Are you positive about that? semicolons at the ends of some lines will break
the code. Consider:

for (i=0;i<31;i++);
{;
alert(i);
};

I use that format when nesting for loops (and if statements) so I can keep
track of opened and closed { }. The semicolon at the end of the for statement
utterly breaks it. Without the ;, you get 31 alerts, starting from 0 to 30, as
it should be. With the semicolon there, you get one alert (31), which is not
what the code is intended to do.

You are correct. Brent should have said that statements should end with
semicolons. JSLINT can help you place them correctly. You also demonstrated why
if statements should always be used with blocks, and why blocks should not be
used alone. JSLINT checks for those, too.

http://www.crockford.com/javascript/lint.html
 
M

Michael Winter

McKirahan wrote on 18 Dec 2003 at Thu, 18 Dec 2003 15:22:10 GMT:
Sorry to be picky but...

"now.getMonth()" returns 0 to 11 (not 1 to 12).

You example will display the previous month.

Very good point! I always forget that...

Mike
 
C

Chris Smith

Douglas said:
You are correct. Brent should have said that statements should end with
semicolons. JSLINT can help you place them correctly. You also demonstrated why
if statements should always be used with blocks, and why blocks should not be
used alone. JSLINT checks for those, too.

Actually, it was me (not Brent) that said lines when I should have said
statements.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Michael Winter

McKirahan wrote on 18 Dec 2003 at Thu, 18 Dec 2003 16:14:43 GMT:
Sorry, "is be" should have been "is being".

<snipped vast amounts of unneeded quoting>

Please, will you both:

1) Not top-post (place your comments under or amongst quoted text)
2) Trim quoted material to only what is relavant to your post

I'm not trying to be awkward; these practices are part of
established Usenet etiquette.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top