Forums
This topic is locked
Autofill formfields based on checkbox
Replies
Replied 30 Jun 2006 09:18:58
30 Jun 2006 09:18:58 sup ert replied:
I worked it out in javascript, found help here;
www.htmlcodetutorial.com/forms/_INPUT_onClick.html
sup
www.htmlcodetutorial.com/forms/_INPUT_onClick.html
sup
Replied 30 Jun 2006 09:37:58
30 Jun 2006 09:37:58 Roddy Dairion replied:
for those who might get this problem again.
In the script where it says form you have to replace this with your form name present in your tag <form>
where is says default1, default2, default3(change these to the field name in your form) then this is the field that will be used as "to be copied", i.e. all value entered here will be copied straight to field1, field2, field3(change these to the field name in your form) IF you click(checked) in the checkbox, the function copy will be executed where all data in default1-3 fields will be copied to field1-3 fields, IF you click again (unchecked) this will have the reverse effect i.e. it will remove everything in the field1-3.
<pre id=code><font face=courier size=2 id=code>
<SCRIPT LANGUAGE=JavaScript>
<!--
function copy()
{
var default1;
default1 = document.form.default1.value;
var default2;
default2 = document.form.default2.value;
var default3;
default3 = document.form.default3.value;
if(document.form.same.checked == true)
{
document.form.field1.value= default1;
document.form.field2.value= default2;
document.form.field3.value= default3;
}
else if(document.form.same.checked == false)
{
document.form.field1.value= "";
document.form.field2.value= "";
document.form.field3.value= "";
}
}
//-->
</SCRIPT>
<form name="form">
<input type="text" id="default1">
<input type="text" id="default2">
<input type="text" id="default3">
Field 1: <input name="field1" type="text" id="field1">
Field 2: <input name="field2" type="text" id="field2">
Field 3: <input name="field3" type="text" id="field3">
<input type="checkbox" name="same" onClick="javascript:copy()">
</form>
</font id=code></pre id=code>
In the script where it says form you have to replace this with your form name present in your tag <form>
where is says default1, default2, default3(change these to the field name in your form) then this is the field that will be used as "to be copied", i.e. all value entered here will be copied straight to field1, field2, field3(change these to the field name in your form) IF you click(checked) in the checkbox, the function copy will be executed where all data in default1-3 fields will be copied to field1-3 fields, IF you click again (unchecked) this will have the reverse effect i.e. it will remove everything in the field1-3.
<pre id=code><font face=courier size=2 id=code>
<SCRIPT LANGUAGE=JavaScript>
<!--
function copy()
{
var default1;
default1 = document.form.default1.value;
var default2;
default2 = document.form.default2.value;
var default3;
default3 = document.form.default3.value;
if(document.form.same.checked == true)
{
document.form.field1.value= default1;
document.form.field2.value= default2;
document.form.field3.value= default3;
}
else if(document.form.same.checked == false)
{
document.form.field1.value= "";
document.form.field2.value= "";
document.form.field3.value= "";
}
}
//-->
</SCRIPT>
<form name="form">
<input type="text" id="default1">
<input type="text" id="default2">
<input type="text" id="default3">
Field 1: <input name="field1" type="text" id="field1">
Field 2: <input name="field2" type="text" id="field2">
Field 3: <input name="field3" type="text" id="field3">
<input type="checkbox" name="same" onClick="javascript:copy()">
</form>
</font id=code></pre id=code>