473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

update text field based on selection field

ok, this is gonna seem stupid, but it has been stumping me all
afternoon.

I have two fields on a HTML page. HTML looks like this...

<html>
<head>
<title>New Page 3</title>
</head>
<body>
<form method="POST" action="index.asp">
<select size="1" name="shiptype" onchange="crazyJavascriptFuntion()">
<option selected>SELECT ONE</option>
<option value="3">GROUND (3 DAY)</option>
<option value="2">2ND DAY AIR (2 DAY)</option>
<option value="1">OVERNIGHT (1 DAY)</option>
</select>
<input type="text" name="duedate" size="20"></p>
</form>
</body>
</html>

I would like for the text field to automatically update to todays date
+ appropriate selection from ship type.

so if someone selects "GROUND" the text box called "duedate" will
update to Today + 1 day.

any help on this would be greatly appreciated.

Jul 23 '05 #1
13 6534
pk*******@hotmail.com wrote:
I would like for the text field to automatically update to todays date
+ appropriate selection from ship type.


A simple approach is ...

<script type="text/javascript">
function setDueDate(oSel) {
var nNow = new Date().getTime(),
nOff = oSel.options[oSel.selectedIndex].value * 8.64E7,
// 86400000 milliseconds are one day
dDue = new Date(nNow + nOff);
oSel.form.elements["duedate"].value
= dDue.getFullYear() + "/"
+ digits(dDue.getMonth() + 1, 2) + "/"
+ digits(dDue.getDate(), 2);
}
function digits(s, n) {
s = s.toString();
while (s.length < n) s = "0" + s;
return s;
}
</script>
[...]
<select name="shiptype" onchange="setDueDate(this)">

.... but this depends on correct date settings at client's system.
If your web server supports apache style Server Side Includes (SSI),
you can simply avoid that using a litte SSI extension:

<!--#config timefmt="%a, %d %b %Y %T GMT"-->
<script type="text/javascript">
var timeOffset = new Date().getTime()
- new Date("<!--#echo var="DATE_GMT" -->").getTime();
function getServerTime() {
return new Date(new Date().getTime() - timeOffset);
}
function setDueDate(oSel) {
var nNow = getServerTime(),
nOff = oSel.options[oSel.selectedIndex].value * 8.64E7,
dDue = new Date(nNow + nOff);
oSel.form.elements["duedate"].value
= dDue.getFullYear() + "/"
+ digits(dDue.getMonth() + 1, 2) + "/"
+ digits(dDue.getDate(), 2);
}
function digits(s, n) {
s = s.toString();
while (s.length < n) s = "0" + s;
return s;
}
</script>
[...]
<select name="shiptype" onchange="setDueDate(this)">

ciao, dhgm
Jul 23 '05 #2
Dietmar Meier wrote:
[...]
function digits(s, n) {
s = s.toString();
while (s.length < n) s = "0" + s;
return s;
}


This can be a tad simpler:

// Add leading zero to numbers less than 10
function digits(x) {
return (x<10)? x = "0" + x : x;
}

--
Fred
Jul 23 '05 #3
Fred Oz wrote:
function digits(s, n) {
s = s.toString();
while (s.length < n) s = "0" + s;
return s;
}
This can be a tad simpler:
Yes (but digits(s, n) is more versatile).
// Add leading zero to numbers less than 10
function digits(x) {
return (x<10)? x = "0" + x : x; ^^^^
Drop that "x = ".
}


Another simple and fast one:

function digits2(s) {
return (Number(s) + 100).toString().substring(1);
}

ciao, dhgm
Jul 23 '05 #4
thanks a TON you guys. i ended up with the following, which i had
bastardized from some other javascript i found. its only works with
..ASP pages, but it works...

<script language='javascript'>
function SelVal(fF,tF,fname,tname)
{
eval(tF + "." + tname + ".value = " + fF + "." + fname +
".options.value");
}
</script>

<form name="mess1" ID="Form1">
<table cellpadding="4" border="0" ID="Table2">
<tr>
<td>
<p><b>First</b></p>
</td>
<td>
<p><b><select style="width=140" name="us1"
onchange="SelVal('mess1','mess2','us1','sf1')" ID="Select1">
<option value selected>SELECT ONE
<option value="<%=NOW()+4%>">Ground (4 day)
<option value="<%=NOW()+2%>">2ND DAY (2 day)
<option value="<%=NOW()+1%>">OVERNIGHT (1 day)
</select></b></p>
</td>
</tr>
</table>
</form>
<form name="mess2" ID="Form2">
<table cellpadding="4" border="0" ID="Table3">
<tr>
<td>
<p><b>Second</b></p>
</td>
<td>
<p><input type="text" name="sf1" size="40" ID="Text2"></p>
</td>
</tr>
</table>
</form>

its ugly, its messy, but it does exactly what i wanted it to. thanks a
TON for your insight guys. probably gonna end up going with one of the
above solutions, just cause they look better and aren't .asp dependent.

Jul 23 '05 #5
pk*************@hotmail.com wrote:
<script language='javascript'>
function SelVal(fF,tF,fname,tname)
{
eval(tF + "." + tname + ".value = " + fF + "." + fname +
".options.value");
}


This works in MSIE only, and is an extremely useless use of evil eval.
You should replace it with:

<script type="text/javascript" language='javascript'>
function SelVal(sel, tF, tname) {
var f, t;
if ((f = document.forms[tF]) && (t = f.elements[tname]))
t.value = sel.options[sel.selectedIndex].value;
}
</script>
....
<select [...] onchange="SelVal(this, 'mess2', 'sf1')" [...]>

ciao, dhgm
Jul 23 '05 #6
JRS: In article <35*************@individual.net>, dated Tue, 25 Jan
2005 23:34:01, seen in news:comp.lang.javascript, Dietmar Meier
<us***************@innoline-systemtechnik.de> posted :
var nNow = new Date().getTime(),
nOff = oSel.options[oSel.selectedIndex].value * 8.64E7,
// 86400000 milliseconds are one day Usually. dDue = new Date(nNow + nOff);


Will probably fail if the interval spans the last Sunday in either March
or October, probably March.

nNow = new Date()
nOff = oSel.options[oSel.selectedIndex].value
dDue = nNow.setDate(nNow.getDate() + nOff)

See below.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #7
JRS: In article <41f6f494$0$10525$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Wed, 26 Jan 2005 11:33:42, seen in
news:comp.lang.javascript, Fred Oz <oz****@iinet.net.auau> posted :
// Add leading zero to numbers less than 10
function digits(x) {
return (x<10)? x = "0" + x : x;
}


Unfortunately its result can be either a string or a number. That can
have unfortunate consequences if

YYYYMMDD = digits(D.getFullYear()) +
digits(D.getMonth()+1) +
digits(D.getDate())

is tested only in Jan..Sep. And the "x = " is not needed. Consider

return ( x<10 ? "0" : "") + x

See below.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #8
Dr John Stockton wrote:
// 86400000 milliseconds are one day
Usually.
Always (apart from leap seconds). DST simply changes the time zone,
the length of a day (as Date.getTime() shows) is not really affected
by this.
dDue = new Date(nNow + nOff);

Will probably fail if the interval spans the last Sunday in either
March or October, probably March.


Yep

ciao, dhgm
Jul 23 '05 #9
JRS: In article <35*************@individual.net>, dated Wed, 26 Jan
2005 09:35:34, seen in news:comp.lang.javascript, Dietmar Meier
<us***************@innoline-systemtechnik.de> posted :

function digits2(s) {
return (Number(s) + 100).toString().substring(1);
}

I tested something similar to that, and found it slower (on a reasonable
input mix) than conditionally adding "0" / ""; however, it seemed to be
the best algorithm for VBScript.

It's hardly critical; but it might be amusing to see what is really
best.
NOTE : There are two cases.

(1) the parameter is guaranteed to be an integer in the range 0..99 -
which is so for much common use.
(2) it is not - in that case my preference is for a method that will
always return the correct numerical value. If it's going to be wrong,
it should *look* wrong!

Consider these, which I have not timed :

function D2(X) { var S = X+""
return S.length==1 ? "0"+S : S }

function D2(X) { var S
return (S=X+"").length==1 ? "0"+S : S }

and note that they work equally well if the number is a hex string -
i.e. a -> 0a !

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 23 '05 #10
JRS: In article <35*************@individual.net>, dated Thu, 27 Jan
2005 01:00:23, seen in news:comp.lang.javascript, Dietmar Meier
<us***************@innoline-systemtechnik.de> posted :
Dr John Stockton wrote:
// 86400000 milliseconds are one day

Usually.


Always (apart from leap seconds). DST simply changes the time zone,
the length of a day (as Date.getTime() shows) is not really affected
by this.


Date.getTime() has nothing to do with Days; it is an absolute time
measure, giving the same result simultaneously world-wide, for well-
adjusted systems. It is a pity that the identifier getUTCms was not
used instead.

The last Sunday in March has 23 hours, from 00:00 to 24:00.
The last Sunday in October has 25 hours, from 00:00 to 24:00.
North Americans are later in Spring, Antipodeans are inverted, others
vary.

ISO 8601:2000 considers the day as a unit of time to be 24 hours; but
the calendar day to be 24 hours +- leap second +- other (Summer Time)
changes.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 23 '05 #11
Dr John Stockton wrote:
ISO 8601:2000 considers the day as a unit of time to be 24 hours; but
the calendar day to be 24 hours +- leap second +- other (Summer Time)
changes.


Agreed.

ciao, dhgm
Jul 23 '05 #12
Dr John Stockton wrote:
It's hardly critical; but it might be amusing to see what is really
best.


That's the kind of amusement my wife doesn't understand ...

For your gallery another funny one (even faster, especially in Geckos):

function d2tf5(n) {
return (- -n < 10 && "0" || "") + n;
}

ciao, dhgm
Jul 23 '05 #13
JRS: In article <35*************@individual.net>, dated Fri, 28 Jan
2005 01:32:49, seen in news:comp.lang.javascript, Dietmar Meier
<us***************@innoline-systemtechnik.de> posted :
Dr John Stockton wrote:
It's hardly critical; but it might be amusing to see what is really
best.


That's the kind of amusement my wife doesn't understand ...

For your gallery another funny one (even faster, especially in Geckos):

function d2tf5(n) {
return (- -n < 10 && "0" || "") + n;
}


For me, as is, it is just a little faster than the LZ I've been using.

But it gives unfortunate results for x<0, and fixing that removes the
speed difference for me.

Starting -n > -10 saves one character!

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mark A Framness | last post by:
Greetings, I am working on a project and we need to write a conversion script to initialize a new field on a table. The number of records on this table is on the order of millions so routine...
6
by: Steve Speirs | last post by:
Hi I'm trying to show/hide a simple piece of text and a text field on a form based on what choice is made from a drop down box. <select name="dropdown" size="1"> <option selected...
1
by: John Doe | last post by:
I have a PHP page that generates a list box with several options in it. I would like to have a "view" link next to the list box. When the user changes the contents of the list box, I would like...
1
by: OhFiddleSticks | last post by:
Does anyone know if there is a way to add a text value to a combo box (the text box part, not the rowsource) in VBA without triggering an update event? I've tried everything I can think of without...
11
by: Siv | last post by:
Hi, I seem to be having a problem with a DataAdapter against an Access database. My app deletes 3 records runs a da.update(dt) where dt is a data.Datatable. I then proceed to update a list to...
6
by: slavisa | last post by:
I have one questions about my database. I have made couple of tables and some forms and a report. I have a table called CourseNumbers with 2 fields, one is the coursenumber (ex. CI-120) and other...
1
by: peasedm | last post by:
Okay this one has me stumped. I have a table called Review_Statements with the following columns: statementid type statement1 statement2 statement3 I have a form called SR_Review with an...
9
by: Brett_A | last post by:
I have a form where the first field is a dynamic drop-down that pulls from a db (Access). The fields associated with the query are task_id, task_name and task_rate. The field has the value of...
1
by: Rosie | last post by:
I have a main form with header info w/ 'tHeader' as the control source. I have a subform with 'tDetail' as a control source. They're strung together by a field named MA_ID. This works...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.