f



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.

0
pkinville (1)
1/25/2005 9:16:01 PM
comp.lang.javascript 38370 articles. 2 followers. javascript4 (1315) is leader. Post Follow

13 Replies
923 Views

Similar Articles

[PageSpeed] 17

pkinville@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
0
Dietmar
1/25/2005 10:34:01 PM
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
0
Fred
1/26/2005 1:33:42 AM
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
0
Dietmar
1/26/2005 8:35:34 AM
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.

0
pkinvilleNOSPAM
1/26/2005 2:32:16 PM
pkinvilleNOSPAM@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
0
Dietmar
1/26/2005 3:24:04 PM
JRS:  In article <35nvpsF4o9nh9U1@individual.net>, dated Tue, 25 Jan
2005 23:34:01, seen in news:comp.lang.javascript, Dietmar Meier
<usereplytoinstead@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.
0
Dr
1/26/2005 7:24:25 PM
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 <ozfred@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.
0
Dr
1/26/2005 7:34:40 PM
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
0
Dietmar
1/27/2005 12:00:23 AM
JRS:  In article <35p31pF4iop0mU1@individual.net>, dated Wed, 26 Jan
2005 09:35:34, seen in news:comp.lang.javascript, Dietmar Meier
<usereplytoinstead@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.
0
Dr
1/27/2005 2:41:29 PM
JRS:  In article <35qp7pF4qodn9U1@individual.net>, dated Thu, 27 Jan
2005 01:00:23, seen in news:comp.lang.javascript, Dietmar Meier
<usereplytoinstead@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.
0
Dr
1/27/2005 2:51:08 PM
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
0
Dietmar
1/27/2005 7:09:30 PM
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
0
Dietmar
1/28/2005 12:32:49 AM
JRS:  In article <35tfghF4rr6ktU1@individual.net>, dated Fri, 28 Jan
2005 01:32:49, seen in news:comp.lang.javascript, Dietmar Meier
<usereplytoinstead@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.
0
Dr
1/28/2005 5:09:35 PM
Reply:

Similar Artilces:

Can JavaScript select text in a text-field?
Hi, I would like to have a text-field (in html form) with a selected text. In other words, it user loads the form, he/she can see a text in the text field (this text should explain what should be put in the text field). It is convenient to have a selected text, so that it user start to write something in the text filed the selected text will be immediately deleted. Thank you in advance for any help. On 2009-01-20 02:07, Kurda Yon wrote: > I would like to have a text-field (in html form) with a selected text. You can use fieldElement.focus() and fieldElement.select(). - Conrad ...

trying to disable a text input field when a particular select list option is selected in a select field but this code has no effect
<script> $(document).ready(function () { $('#select_field').change(function(e) { if (e.target.value=='select_value') {e.second_field.disabled = true;} }) }) </script> On 2015-11-30 05:23, fugee279@gmail.com wrote: > <script> > $(document).ready(function () { > $('#select_field').change(function(e) > { > if (e.target.value=='select_value') {e.second_field.disabled = true;} > }) > }) > </script> Next time, please include the error message you see in the console. The para...

Updating field based on Value of another field
Can somebody help me with this scenario? I want to update a date field when another field has a certain value entered. It is something like this: When the PDetType field ( from tlkpERegStatus and tblPmtDetail) has a value of "DO" ("Donation") or "SC" ("Scholarship Contribution") entered, update the field NWCInvolveDate in the tblNWContact with the current date. Thanks in advance. You could do this in a form and use the PDetType control's AfterUpdate event. Otherwise, you can do this at the table level with a query that scans PDetType and updates...

Update a field color based on field contents
Hi guys, I'm using a subform to display a list of values from a single table. The form is showing the values in "continuous forms" view and there will only ever be two different values for the form. The form show 2 fields, 1 is a number and the second is a calculated field that says either "Valid" or "Invalid" based on the first fields contents. What I need to do is if the value is Invalid then I'd either like the text or the actual field to be red so it stands out from the other fields but if the value is Valid then I want the field to be standard (i.e...

Update Table or Form Field Based on Previous Field
I'm sure this has been covered before in the newsgroup but have had no luck locating it. I have two tables: InstMonitors & MonModDesc Structure of MonModDesc: Model, Description Structure of InstMonitors: CompName, Make, Model, Description, Serial, PurchDate, WarrExpDate The MonModDesc table is simply a listing of the Monitor Model Numbers and associated Description with that Model Number. The InstMonitors table is a tracking of activity of the specific monitor: CompName is the name of the computer it is assigned to. Make is the brand name of the Monitor. Model is selected fro...

Updating A Text String In A Text Field With An Update Query
If I have the following string in a text field of a table: "This is a test" can I create an update query that will change, say, the string "is" to "xyz" in the two locations where it occurs or would this require VBA? Thanks, Sheldon Potolsky Use an Update query. Something like this: UPDATE Table1 SET [F1] = Replace([F1], "is", "xyz") WHERE [F1] Like "*is*"; -- Allen Browne - Microsoft MVP. Perth, Western Australia Tips for Access users - http://allenbrowne.com/tips.html Reply to group, rather than allenbrowne at mvps ...

Pass variable selected from drop down option field to text fields
Can anyone offer suggestions on how to do this or if it is possible? I have a form that uses a drop down box and 2 text fields. What I am trying to do is have the value of each text box set by the choice from the drop down box. Something like: <form name="populatefrm" id="contactfrm" method="post" action="results.asp"> <select name="region" size="10"> <option value="Choice 1" JavaScript "SET Text Box 1 to 12:00; SET Text Box 2 to 5:00">Choice 1</option> <option value="Choice 2...

Updating a field's value based on another field
Hi all, I was wondering if any of you guys can help me out with this: I have two fields on a form: one field is Premium and the other is Brokerage. Whatever amount is in the Premium field is multiplied by 10% in the Brokerage field. If I change the amount in the Premium field, the Brokerage amount should change accordingly as well. I know I can do this in a query with a simple calculation set up; however, there are exceptions to the 10% rule. Therefore, I'll need the ability to change the amount in the Brokerage field from time to time - with a Query, that won't be possible. What is ...

select a few columns from file where field seperator is the same as character in field text
I want to extract fields 3,4 and 8 from a file which looks like below: AAA ,01,001,"XX,YY ",5,10, "AAAA ", 10.25 BBB ,01,002,"XXX,YY ",15,16, "AAAA ", 10.25 CCC ,00,003,"XXYY ",63,156, "AA'BB ", 11 DDD ,00,004,"XXYY.ZZ ",5,10, "AA'CC ", 09.99 Cut command does not work as the field seperator (comma) is part of field text for field 4 in a few lines. The table in which I am inserting the fields should look like below: Table Field1 Field2 Field3 001 XX,YY 10....

Separating one field into two fields based on a character in the field
I know there has to be a way to do this, but I've gone brain dead. The scenario..a varchar field in a table contains a date range (i.e. June 1, 2004 - June 15, 2004 or September 1, 2004 - September 30, 2004 or...). The users have decided thats a bad way to do this (!) so they want to split that field into two new fields. Everything before the space/dash ( -) goes into a 'FromDate' field, everything after the dash/space goes into the 'ToDate' field. I've played around with STRING commands, but haven't stumbled on it yet. Any help at all would be appreciated! D...

Transaction based model updates vs. field based model updates
When one is building a GUI form is it best to update the model after the user enters all of the data on the form or after each field value changes? I have a situation where a change to one field impacts another field on the form. I thought that it would be best to have the model handle the constraint between the two attributes and then have the gui fields observe the model. However, If I use a transaction based approach to update the model then the constraint between the two fields could be in an invalid state when one of them changes. The invalid state occurs because the model doesn't...

Trigger to update field in new record based on second field in new record.
Here is a SQL statement that I am currently running: update timecard set tudef1='JP0' where tudef5='JP' and tmatter in (select mmatter from matter where mloc='30') I would like this update to happen when a new record is inserted in the timecard table. How can I set up a trigger to do this. Thanks in advance Rich Richard (richard.mogy@gmail.com) writes: > Here is a SQL statement that I am currently running: > > update timecard set tudef1='JP0' where tudef5='JP' and tmatter in > (select mmatter from matter where mloc='30') > ...

Update Query
I have about 20,000 records pulled from Excel that I need to update. What I need to do is run an update query that bascially says: If a field is null, update it with the previous record value of that same field. In some instances, it will have to go back a few records before it finds a value that is not null. Can this be done? Thanks Bill This example assumes: - the table is named "MyTable"; - the field to be updated is named "MyField"; - the primary key field is named named "ID". 1. Create a query into this table. 2. Drag MyField into the grid. 3. In the...

javascript validation for a not required field, field is only required if another field has a value
The psuedocode is 1. if "search_bu" is empty then return false because it is a required field 2. if "search_zoneid has a value then if search_zonenm is empty return false 3 OR the reverse of 2 if "search_zonenm has a value then if search_zoneid is emtpy return false I think what it is doing know is working on all 3 but if I don't input either a zoneid or zonenm it still asks for the zonenumber????? It may be number 2 and 3 need to be an OR someway? The problem is zoneid or zonenm are not rquired but it is acting like the...

Web resources about - update text field based on selection field - comp.lang.javascript

Selection - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Cricket: ACT Comets selection trial unearths young talent pushing for Futures League spots
... Creek Molonglo fast bowler Joe Slater (3-31) also stood up, catching the attention of Comets senior coach Kyle Piper. "Each of the selection ...

Jury selection to begin for Tim Bosma murder trial
Jury selection is set to begin today in Hamilton at the trial of two men accused in the death of Tim Bosma. Police say Bosma left his home on ...

Lloyd Carr Among 4 Named to College Football Playoff Selection Committee - Bleacher Report
Four new members were appointed to the College Football Playoff selection committee Thursday, including former University of Michigan head coach ...

Jeff Long no longer chair of College Football Playoff Selection Committee
Arkansas athletic director Jeff Long's tenure as chairman of the College Football Playoff Selection Committee will end after two seasons.

Selection of Awesome Russian Jets from 2015
Russian photographer and a blogger, Mike S., decided to post his best shots of the previous year and it came out to be pretty brilliant collection. ...

Jury selection set in fatal 2014 Bourbon Street gunfight - Fox News
A trial is scheduled to begin in New Orleans for the only person arrested following a 2014 gunfight on Bourbon Street that left a bystander dead ...

Jury Selection Starts in Prison Contraband Drone Case
Jury selection is underway for the trial of a Maryland man accused of planning to use a drone to fly drugs, tobacco and pornography into a maximum-security ...

Lloyd Carr, Rob Mullens, Jeff Bower, Herb Deromedi named to College Football Playoff selection committee ...
Former Michigan coach Lloyd Carr, Oregon AD Rob Mullens, ex-Southern Miss coach Jeff Bower and former Central Michigan coach Herb Deromedi will ...

College Football Playoff Selection Committee Appoints Four New Members
The College Football Playoff Selection Committee appointed four new members on Thursday, as reported on by ESPN . Former University of Michigan ...

Resources last updated: 1/24/2016 4:56:29 PM