View Full Version : How to copy value from one textfield to another
trondulseth
08-28-2002, 01:17 PM
Is there a way I can copy the value from one textfield to another by clicking a button. Or even better - a way to copy the values from a set of textfields to another set of textfields.
beetle
08-28-2002, 01:46 PM
Sure thing<script>
function copyTA(f) {
f.elements['target'].value = f.elements['source'].value;
}
</script>
<form>
<textarea id="source">Text to be copied</textarea>
<br>
<textarea id="target"></textarea>
<br>
<input type="button" value="Copy Text" onClick="copyTA(this.form);">
<form>There is a better way to do this for multiple copies, but I haven't the time at the moment. I'll try to get at it later today :D
trondulseth
08-28-2002, 01:52 PM
..... I'll try to get at it later today :D
Thanx a lot - I can hardly wait :o
jalarie
08-28-2002, 01:53 PM
<script type="text/javascript">
<!-- Hide this code from non-JavaScript browsers
function CopyIt(Which) {
Which.to1.value=Which.from1.value;
Which.to2.value=Which.from2.value;
}
// End hiding -->
</script>
<form>
<input type="text" name="from1">
<input type="text" name="to1">
<input type="text" name="from2">
<input type="text" name="to2">
<input type="button" value=" Copy " onclick="CopyIt(this.form);">
</form>
beetle
08-28-2002, 02:34 PM
Ok, here it is. Notice how I included three buttons to create three different results, as an illustration of how flexible the function is.<html>
<head>
<title>Test</title>
<script>
function copyTA(f) {
for (var i = 1; i<arguments.length; i+=2)
f.elements[arguments[i+1]].value = f.elements[arguments[i]].value;
}
</script>
</head>
<body>
<form>
<textarea id="source1">Text to be copied 1</textarea><textarea id="target1"></textarea>
<br>
<textarea id="source2">Text to be copied 2</textarea><textarea id="target2"></textarea>
<br>
<textarea id="source3">Text to be copied 3</textarea><textarea id="target3"></textarea>
<br>
<textarea id="source4">Text to be copied 4</textarea><textarea id="target4"></textarea>
<br>
<textarea id="source5">Text to be copied 5</textarea><textarea id="target5"></textarea>
<br>
<input type="button" value="Copy All" onClick="copyTA(this.form,'source1','target1','source2','target2','source3','target3','source4','target4','so urce5','target5');"><br>
<input type="button" value="Copy Odds" onClick="copyTA(this.form,'source1','target1','source3','target3','source5','target5');"><br>
<input type="button" value="Copy Evens" onClick="copyTA(this.form,'source2','target2','source4','target4');"><br>
<form>
</body>
</html>Arguments must be sent to the function in pairs, with the source field being the first, and the target field being the 2nd
trondulseth
08-29-2002, 09:54 AM
It works like a charm :thumbsup:
One last question: can it be made so that the source fields will be emtied when copied over to the target fields?
beetle
08-29-2002, 10:35 AM
Well sure! just add this line to the functionfunction copyTA(f) {
for (var i = 1; i<arguments.length; i+=2) {
f.elements[arguments[i+1]].value = f.elements[arguments[i]].value;
f.elements[arguments[i]].value = "";
}
}
trondulseth
08-29-2002, 11:07 AM
GREAT!!! :D
Thanx a lot Beetle.
:thumbsup: :thumbsup: :thumbsup:
beetle
08-29-2002, 11:19 AM
You bet, I'm glad it works for ya. Now, the real question of the day is, "Do you understand it?" :D
trondulseth
08-29-2002, 11:29 AM
Hehe - You had to ask :)
Well yes I do - It's just that being a server side and database programer I'm not to familiar with the posibilities and syntax of client side scripting.
Of course I should have made the effort to learn JS, but the time....
A big pat on the shoulders to all you helpfull souls out there.
ps - If you ever go to the ColdFusion forums (should you ever get involved with that - something I highly recomend) on the Macromedia site, it might be I'll be able to help you right back. It's all about the giving and the taking (seen that one on Friends :o )
trondulseth
08-29-2002, 11:36 AM
btw - how do I get this function to work on <input type="text"> instead of <textarea>
I've tried setting the id's the same, but it won't work. Here's what I have:
<input id="uke_1" name="uke_1" type="text" value="38" size="2">
<input id="uke_2" name="uke_2" type="text" value="39" size="2">
<input name="button" type="button" onClick="copyTA(this.form,'uke_2','uke_1');" value=" << ">
beetle
08-29-2002, 02:30 PM
The way you ahve it setup it will copy from uke2 to uke1. Rember the order the argument pairs have to be in is source THEN target.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.