/    Sign up×
Community /Pin to ProfileBookmark

"Object expected" error driving me crazy!

Can anyone find the reason I’d be getting an “object expected” error in the line marked in green? It’s getting to the point of infuriating!

[CODE]
<html>
<head>
<script language=”text/javascript”>

function PutDate()
{
var d = new Date();
var yr = d.getFullYear();
var year1 = yr.charAt(2)+””;
var year2 = yr.charAt(3)+””;
var year = year1 + year2;

document.input.mon.value = d.getMonth() + 1;
document.input.year.value = year;
document.input.day.value = d.getDate();
}
</script>
<title>My Page</title>
</head>
<body>

<form enctype=”multipart/form-data” action=”” method=”post” name=”input”>
<input type=”text” name=”mon” size=”1″ maxlength=”2″>/
<input type=”text” name=”day” size=”1″ maxlength=”2″>/
<input type=”text” name=”year” size=”2″ maxlength=”4″>&nbsp;
[COLOR=”Green”]<button onClick=”PutDate()”>Today</button>[/COLOR]
</form>

</body>
[/CODE]

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@TheBearMayJan 03.2009 — Input is a reserved word. Change your form name and associated references and see if that helps. Haven't tested, but suspect that you're getting an error when it tries to process document.input....
Copy linkTweet thisAlerts:
@astupidnameJan 03.2009 — Try it like this:

[code=html]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>My Page</title>
<script type="text/javascript">

function PutDate()
{
var d = new Date();
var yr = d.getFullYear().toString(); //convert date object to string
var year1 = yr.charAt(2)+"";
var year2 = yr.charAt(3)+"";
var year = year1 + year2;

document.myInputs.mon.value = d.getMonth() + 1;
document.myInputs.year.value = year;
document.myInputs.day.value = d.getDate();
}
</script>
</head>
<body>
<form action="" name="myInputs">
<p>
<input type="text" name="mon" maxlength="2" size="1">/
<input type="text" name="day" maxlength="2" size="1">/
<input type="text" name="year" maxlength="4" size="2">&nbsp;
<!-- Don't use non-standard html buttons in forms (FF don't like that), use standard input buttons inside of forms instead -->
<input type="button" onclick="PutDate()" value="Today">
</p>
</form>
</body>
</html>[/code]


Or an even shorter way you could do that script:
&lt;script type="text/javascript"&gt;
function PutDate()
{
var d = new Date();
var yr = d.getFullYear().toString().slice(2);
document.myInput.mon.value = d.getMonth() + 1;
document.myInput.year.value = yr;
document.myInput.day.value = d.getDate();
}
&lt;/script&gt;
Copy linkTweet thisAlerts:
@KorJan 03.2009 — 
<!-- Don't use non-standard html buttons in forms (FF don't like that), use standard input buttons inside of forms instead -->
[/QUOTE]

Are you sure? The BUTTON element is as standard as any other form's element, see:

http://www.w3.org/TR/html401/interact/forms.html#h-17.5

Moreover, the BUTTON element is more versatile than the INPUT element.

I have never heard about "FF does not like the BUTTON element".
Copy linkTweet thisAlerts:
@astupidnameJan 03.2009 — Are you sure? The BUTTON element is as standard as any other form's element, see:

http://www.w3.org/TR/html401/interact/forms.html#h-17.5

Moreover, the BUTTON element is more versatile than the INPUT element.

I have never heard about "FF does not like the BUTTON element".[/quote]


You are correct, Kor. Sorry, I chose my words wrong and was confused by a strange problem I was having with it in FF until I changed to input instead of button. But when I check it again now with button instead of input, it does work fine. Strange, must have been something else I fixed and forgot about. As for my choice of words ('non-standard html buttons'), you are right, I was wrong, but I did not actually intend to say they were not part of the standards, just that you are in a form already, why not use input instead.. after all it saves about one character... Lol

Oh, wait a second here, I just figured out what the problem was and why I said that..

The OP had this button:
<button onClick="PutDate()">Today</button>[/quote]

Note the missing type="button" (non-standard without type attribute) try testing my example in FF 3 with the OP's button missing the type="button" and you will see the problem.


These would be correct (input's one char shorter, nah nah na na nah ? ):

<button type="button" onclick="PutDate();">Today</button>

<input type="button" onclick="PutDate();" value="Today">

There can be other problems with using non-input buttons inside of forms, if not used correctly.

A few quotes from w3schools here http://www.w3schools.com/tags/tag_button.asp (from their xhtml and html tag references ):
Definition and Usage

The <button> tag defines a push button.

Inside a button element you can put content, like text or images. This is the difference between this element and buttons created with the input element.

Always specify the type attribute for the button. The default type for Internet Explorer is "button", while in other browsers (and the W3C specification) it is "submit".[/quote]


The <button> tag is supported in all major browsers.

Important: If you use the button element in an HTML form, different browsers will submit different values. Internet Explorer will submit the text between the <button> and </button> tags, while other browsers will submit the content of the value attribute. Use the input element to create buttons in an HTML form.[/quote]
Copy linkTweet thisAlerts:
@KorJan 03.2009 — Well, yes, in fact you had some part of the reason? I reckon I do often use INPUT element, but for other reasons: it looks like a difficult task to style a BUTTON is a crossbrowser way, compared with an INPUT. And, yes, IE is by far the most out-of-standard browser.
Copy linkTweet thisAlerts:
@mrhooJan 03.2009 — All the browsers EXCEPT IE7 (and below) default to 'submit'- before IE8, IE would default to 'button'. (IE8 now defaults to 'submit'.)

It is good practice (and required for strict validation) to explicitly set the type attribute in a button.
Copy linkTweet thisAlerts:
@dz_boyauthorJan 03.2009 — Thanks to astupidname: your "shorter" code worked. I had tried a version similar to that, but it didn't work-- probably for some other reason, but it's fixed now. Thanks again.

(PS- the <button> thing was a last ditch effort, as the error kept popping up at the button. I've changed it back to <input> which I like more.) ?
×

Success!

Help @dz_boy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...