Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > update text field based on selection field

Reply
Thread Tools

update text field based on selection field

 
 
pkinville@hotmail.com
Guest
Posts: n/a
 
      01-25-2005
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.

 
Reply With Quote
 
 
 
 
Dietmar Meier
Guest
Posts: n/a
 
      01-25-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Fred Oz
Guest
Posts: n/a
 
      01-26-2005
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
 
Reply With Quote
 
Dietmar Meier
Guest
Posts: n/a
 
      01-26-2005
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
 
Reply With Quote
 
pkinvilleNOSPAM@hotmail.com
Guest
Posts: n/a
 
      01-26-2005
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.

 
Reply With Quote
 
Dietmar Meier
Guest
Posts: n/a
 
      01-26-2005
(E-Mail Removed) 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
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      01-26-2005
JRS: In article <(E-Mail Removed)>, dated Tue, 25 Jan
2005 23:34:01, seen in news:comp.lang.javascript, Dietmar Meier
<(E-Mail Removed)> 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.
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      01-26-2005
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 <(E-Mail Removed)> 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.
 
Reply With Quote
 
Dietmar Meier
Guest
Posts: n/a
 
      01-27-2005
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
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      01-27-2005
JRS: In article <(E-Mail Removed)>, dated Wed, 26 Jan
2005 09:35:34, seen in news:comp.lang.javascript, Dietmar Meier
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatic form field update based on previous form value selection Brett_A ASP General 8 04-19-2008 11:13 AM
Placing the selection of a list field in a text field Jerry Manner HTML 5 06-09-2005 01:52 PM
JS comparing innerHTML to text selection (window.getSelection() /document.selection) Andrew Crowe HTML 1 09-13-2004 02:22 PM
display text field based on the selection choices John Davis ASP General 1 09-16-2003 05:00 AM
Show/Hide text and form field based on drop down selection Steve Speirs Javascript 0 06-27-2003 12:19 PM



Advertisments