Click here to Skip to main content
15,935,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi i am using two text box when i enter the value in the one text box and after i pressing the check box the text box value need to copy into other text box how it is possible in Java script

Can u help?
Posted
Updated 2-Feb-20 23:58pm

JavaScript
function populateText()
{
 if(document.getElementById('chechboxid').checked)
 {
  var str=document.getElementById('textbox1id).value;// textbox in which you enter data
  document.getElementById('textbox2id).Value= str;
}
}


call the javascrip function as attribute to checkbox in pageload method of your .aspx page

 pageload()
 {

 checkboxid.attributes.add("onclick","populateText();");
}
 
Share this answer
 
v2
Hi,
You can do like this in javascript,
XML
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
<asp:CheckBox ID="chk" runat="server" Text="Copy" onclick="copyvalue()"/>
<script type="text/javascript">
function copyvalue() {
    var txt1 = document.getElementById("<%= txt1.ClientID %>").value;
    document.getElementById("<%= txt2.ClientID %>").value = txt1;
}
</script>

OR
with jquery
XML
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
    <asp:TextBox ID="txt2" runat="server"></asp:TextBox>
    <asp:CheckBox ID="chk" runat="server" Text="Copy"/>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
     $("#<%= chk.ClientID %>").change(function () { 
        var txt1 = $("#<%= txt1.ClientID %>").val();
        $("#<%= txt2.ClientID %>").val(txt1);
     });
</script>

Hope it helps you.
Thanks.
 
Share this answer
 
Hi,

Lets assume that u have created two text-boxes and a check-box in ASP.net as given below:

XML
<asp:TextBox ID="textBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="textBox2" runat="server"></asp:TextBox>
<asp:CheckBox ID="checkbox1" runat="server" onclick="copyValue(this)" />


You can write a javascript function to copy value from one textbox into the other textbox, and call the same function on “onclick” event of the checkbox. Same function can be used to clear the textbox on unchecking the checkbox. Code is given below.

XML
<script type="text/javascript">
    function copyValue(Chk) {
        if (Chk.checked) {
            var textVal = document.getElementById('<%=textBox1.ClientID%>').value;
            document.getElementById('<%=textBox2.ClientID%>').value = textVal;
        }
        else {
            document.getElementById('<%=textBox2.ClientID%>').value = "";
        }
    }
</script>


Hope this will help.
 
Share this answer
 
Hi Vivek,

Please refer to the code below.it copies txtbx value to txtbx2 if checkbox is checked.

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function chkchange() {
            var txtbx = document.getElementById('<%=txtbx.ClientID%>');
            var txtbx2 = document.getElementById('<%=txtbx2.ClientID%>');
            if (document.getElementById('<%=chk1.ClientID%>').checked) {
                txtbx2.value = txtbx.value;
            }
            else {
                txtbx2.value = '';
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="label" AssociatedControlID="txtbx" Text="Text Box 1:" runat="server" />
            <asp:TextBox ID="txtbx" runat="server"></asp:TextBox>
            <asp:Label ID="label1" AssociatedControlID="txtbx2" Text="Text Box 2:" runat="server" />
            <asp:TextBox ID="txtbx2" runat="server"></asp:TextBox>
            <asp:CheckBox ID="chk1" runat="server" Checked="false" onclick="chkchange()" />
        </div>
    </form>
</body>
</html>
 
Share this answer
 
ASP.NET
<asp:CheckBox ID="CheckBox1" runat="server"
       oncheckedchanged="CheckBox1_CheckedChanged"  AutoPostBack="true"/>
     <asp:TextBox ID="txtshow" runat="server"></asp:TextBox>
   <asp:TextBox ID="txtvalue" runat="server"></asp:TextBox>


C#
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
       {
           if (CheckBox1.Checked == true)
           {
               txtvalue.Text = txtshow.Text;
           }
       }



try this.
 
Share this answer
 

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