...

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



Thermidor
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?
TIA,
Sven

Here's the code:

<html>
<head>
<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
&nbsp;&nbsp;&nbsp;&nbsp;}
}
function txtEnter() {
&nbsp;&nbsp;&nbsp;&nbsp;document.myForm.txtCopy.value += String.fromCharCode(event.keyCode);
}
</script>
</head>
<body onkeypress="return txtEnter();" onkeydown="return delKey();">
<form name="myForm">
txtEnter: <input type="text" name="txtEnter">
<br>
txtCopy: <input type="text" name="txtCopy">
</form>
</body>
</html>

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

Thermidor
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,
Sven



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum