Click here to Skip to main content
15,566,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a textbox and a label..when user writes anything in textbox i need the text in textbox to be immediately updated in label..how to achieve this using javascript?this is my code.and it is not working

<asp:TextBox id="txtbox1" runat="server" onchange="settext()">
XML
<asp:Label ID="Label6" runat="server"ForeColor="White" Width="200px"></asp:Label>


XML
<script>
 function settext()
{
    var name = document.getElementById('<%= txtbox1.ClientID %>').value;
    document.getElementById("Label6").innerHTML = name;
}
</script>
Posted

Try this:
XML
<script>
        function settext(e) {
            document.getElementById("<%= Label1.ClientID %>").innerHTML = e.value;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" onkeyup="settext(this);" onkeydown="settext(this);"></asp:TextBox>
        <asp:Label ID="Label1" runat="server" Text="Label">dd</asp:Label>
    </div>
    </form>
</body>
 
Share this answer
 
There are two ways for it::

1st one is:

ASP.NET
<asp:label id="Label6" runat="server" forecolor="White" width="200px" xmlns:asp="#unknown"></asp:label>


JavaScript
<script>
 function settext()
{
    var name = document.getElementById('<%= txtbox1.ClientID %>').value;
    document.getElementById('<%=Label6.ClientID%>').innerHTML = name;
}
</script>



2nd one is


ASP.NET
<asp:label id="Label6" runat="server" forecolor="White" width="200px" ClientIdMode="Static" xmlns:asp="#unknown"></asp:label>


JavaScript
<script>
function settext()
{
    var name = document.getElementById('&lt;%= txtbox1.ClientID %&gt;').value;
    document.getElementById('Label6').innerHTML = name;
}
</script>
 
Share this answer
 
v2
try onkeypress event of textbox.. :)

JavaScript
function settext(valueData)
{
  
    document.getElementById('<%=Label6.ClientID%>').innerHTML = valueData;
}


HEML

ASP.NET
<asp:textbox id="txtbox1" runat="server" onkeypress="settext(this.value)" >
</asp:textbox>
 
Share this answer
 
v2
Comments
Debabrata_Das 13-Jun-14 6:35am    
my 5!
Nirav Prabtani 13-Jun-14 6:43am    
thank you.. :)
Naga Sindhura 16-Jun-14 3:33am    
Sorryy to ask now,
The output i am getting here is one char back,
like if I enter "Hello", In my label it is displaying as "Hell".
How can I solve this.
Nirav Prabtani 16-Jun-14 9:31am    
Post your code , according to my solution it should not happen.
Naga Sindhura 17-Jun-14 1:00am    
Hi Nirav, Here is my code,
<head runat="server">
<title></title>
<script type="text/javascript">
function settext(valueData) {

document.getElementById('<%=Label6.ClientID%>').innerHTML = valueData;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>text </p>
<asp:textbox id="txtbox1" runat="server" onkeypress="settext(this.value)" >

<asp:Label ID="Label6" runat="server">

</div>
</form>
</body>
</html>
C#
document.getElementById("Label6").innerHTML = name;
}



you are using asp label ,so use same method ,ie, document.getElementById('<%=Label6.ClientID%>').innerHTML to assign data.....
 
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