I need some help with using a return value from a function call to
change the value of a textfield.
ie. window.document.form1.textfield.value=now() does not work.
test.html has a form with the textfield I want to change the value of.
It also has a javascript include js/now.js.
now.js contains one function, now(), which returns a string. If I
alert(now()) it prints the string in a message box. But if I try and
assign the return value of the function to the value of a textfield it
doesn't work. Thanks for all help received.
Darren
[test.html]--start----------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Use return value from function to change textfield value</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<script language="JavaScript" type="text/JavaScript" src="js/now.js">
</script>
<script>
alert(now())
</script>
<form name="form1" method="post" action="">
<input type="text" name="textfield">
<input type="button" name="now" value="="
onClick="window.document.form1.textf
ield.value=now();">
</form>
</body>
</html>
[test.html]--end----------------------------------------------------------
[js/now.js]--start----------------------------------------------------------
function now() {
var d = new Date();
var now
now = d.getDate();
now += "-";
switch ((d.getMonth() + 1)) {
case 1 :
now += "JAN";
break;
case 2 :
now += "FEB";
break;
case 3 :
now += "MAR";
break;
case 4 :
now += "APR";
break;
case 5 :
now += "MAY";
break;
case 6 :
now += "JUN";
break;
case 7 :
now += "JUL";
break;
case 8 :
now += "AUG";
break;
case 9 :
now += "SEP";
break;
case 10 :
now += "OCT";
break;
case 11 :
now += "NOV";
break;
case 12 :
now += "DEC";
break;
};
now += "-";
now += d.getFullYear();
now += " ";
if (d.getHours() > 10)
now += d.getHours();
else now += ("0" + d.getHours());
now +=(":");
if (d.getMinutes() > 10)
now +=(d.getMinutes());
else now += ("0" + d.getMinutes());
return now;
};
[js/now.js]--end----------------------------------------------------------
> <input type="button" name="now" value="="
Hi
Rename the button to something else then "now", using "now" overwrites the
function.
Fred
Works like a charm.
Darren
>switch ((d.getMonth() + 1)) {
> case 1 :
> now += "JAN";
> break;
> ... ... ...
> case 12 :
> now += "DEC";
> break;
>};
now += ["JAN", ... "DEC"][d.getMonth()] // is shorter
>now += "-";
>now += d.getFullYear();
>now += " ";
>
>if (d.getHours() > 10)
>now += d.getHours();
>else now += ("0" + d.getHours());
if (d.getHours() < 10) now += "0" ; now += d.getHours() // is simpler
if ((T = d.getHours()) < 10) now += "0" ; now += T // may be quicker
>now +=(":");
>
>if (d.getMinutes() > 10)
>now +=(d.getMinutes());
>else now += ("0" + d.getMinutes());
Since you add a leading zero twice, better to use a function.
Rather than
now = ...
now += ...
now += ...
you can use
now = ... +
... +
...
--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MSIE 4 ©
<URL:http://www.jibbering.com/faq/> FAQ for comp.lang.javascript by Jim Ley.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.