Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi!

I'm trying to change a textbox text dynamically from another textbox. When i'm writing in the first textbox, the text that i'm writing must appare in the second textbox. Please help!

thanks, sorry for the bad english.
Posted

It's the best to do it at client side in pure Javascript as post back for such a frequent event will make performance unacceptable. Here is how:

HTML
<html>
<head>
<title>Copy text box text on each change</title>

<script type="text/javascript">

function copyText() {
    src = document.getElementById("source");
    dest = document.getElementById("dest");
    dest.value = src.value;
}

</script>
</head>

<body>
   <input type="text" id="source" onKeyPress="copyText()">
   <input type="text" id="dest">
</body>
</html>


It works, tested.
I does not seem to have any practical sense though. Hope this is a mere exercise.

—SA
 
Share this answer
 
v2
Comments
fjdiewornncalwe 7-Jul-11 18:16pm    
Darn tootin' right it'll work. That is the simplest way to do this in on the client side.
Sergey Alexandrovich Kryukov 7-Jul-11 18:20pm    
Performance, don't forget performance. Useless action always looks better with good performance :-)
Thank you, Marcus.
--SA
Espen Harlinn 7-Jul-11 18:23pm    
I agree with Marcus, nice and simple - my 5
Sergey Alexandrovich Kryukov 7-Jul-11 21:37pm    
Thank you, Espen.
--SA
Look into the TextChanged event of the TextBox class. It's very easy to do from there.
 
Share this answer
 
Comments
wizardzz 7-Jul-11 12:41pm    
Nice, just beat me to it!
Mitran Stefanita Alexandru 7-Jul-11 12:45pm    
can you show me the exact code.. pls.. i tried
TextBox2.Text = TextBox1.Text; but it doesn't work
Dave Kreskowiak 7-Jul-11 13:04pm    
Seriously?? That line of code is all there is. You just have to put in the TextChanged event handler for the TextBox that your typing in. You've probably put it in the TextChange event of the Form.

I highly suggest picking up a beginners book on VB.NET. This is a very basic concept that you're missing and until you understand it, and many others, you've going to find it very difficult to write anything under the .NET Framework.
Sergey Alexandrovich Kryukov 7-Jul-11 17:56pm    
No, you did not try it. Can you read? The solution says: TextChanged (!!!)
--SA
Mitran Stefanita Alexandru 7-Jul-11 13:22pm    
For it to work the textbox1 behavior "AutoPostBack" must be set to true, but this doesn't fill the second textbox dynamically, while writing in the textbox1. in this situation i must click the page for it to work, is there a method to fill the textbox2 while writing in the textbox1 ?
XML
<html>
<head>
<script type="text/javascript">
    function copy_data(val){
     var a = document.getElementById(val.id).value
     document.getElementById("copy_to").value=a
    }

    </script>
</head>
<body>
<center>
From:<input type="text" name ="a" id="copy_from" onkeyup="copy_data(this)"/><br>
To:<input type="text" name ="b" id="copy_to"/><br>
</center>
</body>
</html>
 
Share this answer
 
The below code is like for booking Air Line Tickets from Source to Destination and Destination to Source at a time. So no need to fill for return tickets in the textboxes.

XML
<html>
<head>
<script type="text/javascript">
    function copy_data(val){
     var a = document.getElementById(val.id).value
     document.getElementById("copy_toret").value=a
    }
    function copy_dataret(val){
     var a = document.getElementById(val.id).value
     document.getElementById("copy_fromret").value=a
    }
    </script>
</head>
<body><pre>
<center><h1>Forward</h1>
From:<input type="text" id="copy_fromfor" onkeyup="copy_data(this)"/><br>
To:  <input type="text" id="copy_tofor"onkeyup="copy_dataret(this)"/><br>
<h1>Return</h1>
From:<input type="text" id="copy_fromret"/>
<br>
To:  <input type="text" id="copy_toret"/><br>

</center>
</pre>
</body>
</html>
 
Share this answer
 
v2
try this...

XML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function userSubmit() {
            var UI = document.getElementById("txtName").value;
            document.getElementById("txtDupName").innerHTML = UI;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtName" runat="server" onKeyUp="userSubmit()"></asp:TextBox>
        <div id="txtDupName">
        </div>
    </div>
    </form>
</body>
</html>
 
Share this answer
 
if you make an event for Textbox 1, when ever text box 1 changes, it should appear in II box.

C#
private void textBox1_TextChanged(object sender, EventArgs e)
    {
        // This changes text box 2 window text when you type into text box 1.
        textBox2.Text = textBox1.Text;
    }


However, make sure that this event is described in textBox1 definition in your front end code.
 
Share this answer
 
v2
Comments
Mitran Stefanita Alexandru 7-Jul-11 13:09pm    
It Doesn't work, i did exactly how you send, and when i write in textbox1 the text in textbox2 doesn't change... what myth be the problem.
fjdiewornncalwe 7-Jul-11 18:16pm    
This will never work in asp.net because every key stroke will cause a complete post back at which point control focus and response lag along with many other issues come into play.
Member 7697262 8-Jul-11 10:18am    
how abt storing the text of first text box in a session variable and updating both text boxes with this session variable in the event of postback? my bad, i did not notice it would result in post back. i know there should be a smarter way to do it, but this is just a round about method. give it a try and let me know either case.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900