View Full Version : Updating one textbox from another and enabling the delete and backspace keys

12-21-2003, 11:47 PM
Hi All,

In the simple code below there are two text boxes. If you type in the textbox labeled txtEnter, the characters you type will appear in the textbox labeled txtCopy.
I want to enable the backspace and delete keys so that basically the text in txtCopy should match anything typed in txtEnter. Can anyone explain to me why the delKey() function only updates the txtCopy one step behind txtEnter. I need them to match as soon as the text in txtEnter is changed.
Does anyone have any suggestions for how to accomplish this?

Here's the code:

<script language="javascript">
function delKey(){
&nbsp;&nbsp;&nbsp;&nbsp;if (window.event.keyCode == 46 || window.event.keyCode == 8){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.myForm.txtCopy.value = document.myForm.txtEnter.value
function txtEnter() {
&nbsp;&nbsp;&nbsp;&nbsp;document.myForm.txtCopy.value += String.fromCharCode(event.keyCode);
<body onkeypress="return txtEnter();" onkeydown="return delKey();">
<form name="myForm">
txtEnter: <input type="text" name="txtEnter">
txtCopy: <input type="text" name="txtCopy">

12-21-2003, 11:55 PM
Try this:
<form action="#">
<label for="t1">
<input type="text" style="display: block;" id="t1" onkeyup="elements[1].value=value;" onkeydown="elements[1].value=value;" />
<br />
<label for="t2">
<input type="text" style="display: block;" id="t2" />

12-22-2003, 12:32 AM
Thanks for the reply. It works great.
However, when I posted it I simplified things maybe too much. What I really need to accomplish is a scenario with three textboxes where what appears in third is the contents of the second appended to the contents of the first. (And people can use the backspace and delete keys in the first and second textboxes.)
Any suggestions on getting to that point?

Thanks again,

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum