Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

autofill a variable value 2

Status
Not open for further replies.

Extras

Technical User
Nov 16, 2001
232
US
Hello I am using coldfusion to create a field that will allow one to pick a date from a calendar.

However, on this form, I am going to have fields for two dates, so, it would be nice if, after a person entered a value for one field, automatically this value got entered into another field..

This is what the test page looks like...


I was wondering if I could use javascript somehow to insert a value into the Date field 2 that matched the value selected in Date 1?

I am attaching the code that is one the page...the initial value portion of Date 2:
initialvalue="#DateFormat(now(),"mm/dd/yy")#"
is what I need to populate based on the selection from Date 1


I guess the answer that I am looking for is how can I insert a variable whose value is dependent on what is picked in Date1 into the value for Date 2...

I look forward to your insight! I am enclosing the code for the test page below..

<head>
<title>Adding A Calendar Event</title>
<link href=&quot;../Includes/styles.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;>
</head>

<body>

<table>
<tr>
<td width=&quot;150&quot;>
<form action=&quot;test.cfm&quot; name=&quot;frm1&quot; id=&quot;frm1&quot;>

<div align=&quot;center&quot;>Date 1</div>
<br>
<cf_datepicker textboxname=&quot;txtcolor1&quot;
formname=&quot;frm1&quot;
image=&quot;Images/PickDate.gif&quot;
font=&quot;arial&quot;
fontcolor=&quot;gray&quot;
dayheadercolor=&quot;990000&quot;
monthcolor=&quot;EAC6BF&quot;
othermonthscolor=&quot;white&quot;
backcolor=&quot;EFEFEF&quot;
first_use_on_page=&quot;yes&quot;
dateorder=&quot;mm/dd/yy&quot;
language=&quot;english&quot;
hidedivname=&quot;div1&quot;
displayorientation=&quot;right&quot;>

</td>
<td width=&quot;150&quot;>
<div align=&quot;center&quot;>Date 2</div>
<br>
<cf_datepicker textboxname=&quot;txtcolor2&quot;
formname=&quot;frm1&quot;
image=&quot;Images/PickDate.gif&quot;
font=&quot;arial&quot;
fontcolor=&quot;gray&quot;
dayheadercolor=&quot;990000&quot;
monthcolor=&quot;EAC6BF&quot;
othermonthscolor=&quot;white&quot;
backcolor=&quot;EFEFEF&quot;
dateorder=&quot;mm/dd/yy&quot;
initialvalue=&quot;#DateFormat(now(),&quot;mm/dd/yy&quot;)#&quot;
language=&quot;english&quot;
hidedivname=&quot;div1&quot;
displayorientation=&quot;right&quot;>

</td>
</tr>
<tr>
<td colspan=&quot;2&quot;>
<div id=&quot;div1&quot; style=&quot;position: relative; top: 0px; left: 0px; visibility: show;&quot;>
test
</div>
</td>
</tr>
</form>

</table>
 
I would use a javascript 'onChange' event handler to modify the contents of the date in text field 2. A function like this should work ::

function copyDate() {
var curDate = document.forms['formName'].elements['textBoxName'].value;
document.forms['formName'].elements['textBoxName'].value = curDate;
return true;
}

Get the function to fire within the text boxes onChange event handler ::

<input name=&quot;textBoxName&quot; onChange=&quot;copyDate()&quot;>

Hope what you were asking was this simple. I am not too familiar with Coldfusion so this may not fit in with what you want.

SnaveBelac - Adventurer
 
Look for this section of code:
(dynamicdrive script)
Code:
function selectDate(param) {
 var arr   = param.split(&quot;|&quot;);
 var year  = arr[0];
 var month = arr[1];
 var date  = arr[2];
 var ptr = parseInt(date);
 ppcPtr.setDate(ptr);
 if ((ppcSV != null)&&(ppcSV)) {
  if (validDate(date)) {
ppcSV.value = dateFormat(year,month,date);hideCalendar();
urotherfield.value = ppcSV.value;} //*** add this ****
Code:
  else {showError(ppcER[2]);if (ppcTI) {clearTimeout(ppcTI);ppcTI = false;}}}
 else {
  showError(ppcER[1]);
  hideCalendar();}}

2b||!2b
 
I went back to the previous version of the javascript and tried using the copyDate option and that also did not work..

:(

no error this time - just did not fill the value..
 
OH NOOOOO!!!

You did it just like I toll ya..

I was expecting that you would make subs like:

ummm let's say your second form field value
you want filled is
Code:
<input type=&quot;text&quot; name=&quot;dt2&quot;>

then the line you would add would be:
Code:
 document.formname.dt2.value = ppcSV.value;}
and be sure not to add a &quot;}&quot; you just want to
move the existing one...

2b||!2b
 
When I used the copyDate code on a page I had setup with two text boxes it worked fine.

Am I right in thinking that you just want to copy the contents of one form field to another form field?

This function does work it may just need to be implemetned slightly differently in line with your calendar code.

-----------------------
SnaveBelac - Adventurer
-----------------------
 
Be sure to substitute your real form name for

document.formname.dt2.value

Let us know if you don't get this worked out or
if you don't have a handle on the form stuff.

2b||!2b
 
Thanks for all your input - am headed off to a meeting - will promise to respond after i return and test it out!
 
You function and code should look like this - I had a look at the source code through your link and got the names for your form and text fields...


function copyDate() {
var curDate = document.forms['EventAdd'].elements['CalEvent_StartDate'].value;
document.forms['EventAdd'].elements['CalEvent_EndDate'].value = curDate;
return true;
}

<input type=&quot;text&quot; name=&quot;CalEvent_StartDate&quot; class=&quot;textfieldReq&quot; onChange=&quot;copyDate()&quot;>


This should have the desired effect. remeber that Javascript is case sensitive and make sure that all you variable names are correct...



-----------------------
SnaveBelac - Adventurer
-----------------------
 
SnaveBelac,

Will onChange work when the change is made by a script??
I also tried onChange before I started messing with the
calendar script???

Extras,
Went to the site and for some reason it won't except
the usage of &quot;= ppcSV.value&quot; so try these new lines..

Code:
 if ((ppcSV != null)&&(ppcSV)) {
  if (validDate(date)) {ppcSV.value = dateFormat(year,month,date);hideCalendar();
  document.EventAdd.CalEvent_EndDate.value = dateFormat(year,month,date);}


2b||!2b
 
Extras,

You will also want to include a nested &quot;if&quot;
without it the script will change the end
date everytime someone changes the start date,

With these lines if there is a value in the end
date no change is made.
Code:
 if ((ppcSV != null)&&(ppcSV)) {
  if (validDate(date)) {ppcSV.value = dateFormat(year,month,date);hideCalendar();
         if(document.EventAdd.CalEvent_EndDate.value.length == 0) {
  document.EventAdd.CalEvent_EndDate.value = dateFormat(year,month,date);}}

2b||!2b
 
Extras,

This code should now cause the StartDate value to be entered into the EndDate field. Change you pupdate.js script file as follows - IT WILL ONLY WORK FOR THIS PAGE AND THESE TWO FIELDS...

function selectDate(param) {
var arr = param.split(&quot;|&quot;);
var year = arr[0];
var month = arr[1];
var date = arr[2];
var ptr = parseInt(date);
ppcPtr.setDate(ptr);
if ((ppcSV != null)&&(ppcSV)) {
if (validDate(date)) {
ppcSV.value = dateFormat(year,month,date);
document.EventAdd.CalEvent_EndDate.value = dateFormat(year,month,date);
hideCalendar();
}

else {showError(ppcER[2]);if (ppcTI) {clearTimeout(ppcTI);ppcTI = false;}}}
else {
showError(ppcER[1]);
hideCalendar();}}


Lrnmore,

I did not realise that the onChange event does not fire when the contents are modified through a script. Thanks.

Do you have any idea how to cause an event to fire through a script. Can you force an onChange event with a javascript function of any kind??

-----------------------
SnaveBelac - Adventurer
-----------------------
 
Sorry. I have just re-read this thread and noticed that The post before my last one is basically the same as mine.

Take Lrnmore's advice and add the nested if statement.

-----------------------
SnaveBelac - Adventurer
-----------------------
 
Sorry for the delay in getting back...I could not revisit this project until today..

This is just awesome! It works like a charm!

Really-Really appreciate your sticking with a novice through this!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top