Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Updating one textbox from another and enabling the delete and backspace keys

    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>

  • #2
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this:
    Code:
    <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>

  • #3
    New to the CF scene
    Join Date
    Dec 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •