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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy value from one form field to another

Status
Not open for further replies.

tspoll

Programmer
Aug 12, 2009
8
GB
Hi

I have a form with a date field which defaults to today's date, called date1. I also have checkbox, cb1, which I want to have the effect that if it is checked it copies the value of date1 into a second date field date2. The complications are that if the user has overwritten the default value of date1, the checkbox must copy the overwritten value not the default, and if the user checks the checkbox and subsequently changes date1, it is automatically changed in date2. If a user checks the box and later unchecks it, the unchecking must delete the value previously written to date2. Ultimately, date2 will be a hidden field.
i.e. if cb1 is checked, date1 = date2, whatever the order the user has done things. If cb1 is unchecked then date2 is empty.

I'm afraid this is beyond my rudimentary javascript skills. Can anybody assist please?

Thanks in advance for any pointers.

Tim
 
There are too many if's in your statement, which are hard to follow.

Not sure why you need a checkBox to do any thing:

For dateField1 just use the "OnChange" function to copy to dateField2 if it's changed, otherwise dateField2 reamins empty.
 
You'll need 2 functions for that, one that runs when the checkbox is checked or unchecked and transfers the value from the first input to the second one, and the second one that runs when the textbox is altered. this second one will have to check if the checkcbox is checked and do its thing if it is.

The complications are that if the user has overwritten the default value of date1, the checkbox must copy the overwritten value not the default,
[quote
no complication there, that's the normal behavior. Once the default value has been overwritten, the action can not copy it anymore, it will copy the current value of the input whatever it is.
Code:
function chkbox_action(){
var date1_input=document.myformsname.date1;
var date2_input=document.myformsname.date2;

if(document.myformsname.chkboxname.checked){
date2_input.value=date1_input.value;
}

else{
date2_input.value="";
}
}

function date1_change(){
var date1_input=document.myformsname.date1;
var date2_input=document.myformsname.date2;

if(document.myformsname.chkboxname.checked){
date2_input.value=date1_input.value;
}
}


date1_change should be set to run on the onClick event of the checkbox, and the date1_change should be set to run on the onChange event of the date1 input. The value will be updated in the second input when date1 looses focus. If you want a simultaneous change you may want to use the onKeyUp event instead.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks Vacunita - that's just what I needed.

Just for the benefit of anyone following the thread later, it is chkbox_action not date1_change which should be set to run on the onClick event of the checkbox, I think?

Many thanks for your help.

Tim
 
Oops. Yes you are right. Its chkbox_action that needs to run on the onClick event of the checkbox.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top