Click here to Skip to main content
14,218,541 members
Rate this:
Please Sign up or sign in to vote.
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()">
<asp:Label ID="Label6" runat="server"ForeColor="White" Width="200px"></asp:Label>


<script>
 function settext()
{
    var name = document.getElementById('<%= txtbox1.ClientID %>').value;
    document.getElementById("Label6").innerHTML = name;
}
</script>
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 3

Try this:
<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>
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

There are two ways for it::

1st one is:

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


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



2nd one is


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


<script>
function settext()
{
    var name = document.getElementById('&lt;%= txtbox1.ClientID %&gt;').value;
    document.getElementById('Label6').innerHTML = name;
}
</script>
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

try onkeypress event of textbox.. :)

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


HEML

<asp:textbox id="txtbox1" runat="server" onkeypress="settext(this.value)" >
</asp:textbox>
   
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>
faizel s 20-Jun-14 3:06am
   
the code works well but when user clicks on the enter key,the page refreshes and all the data will be lost... :( how to tackle it?
Rate this:
Please Sign up or sign in to vote.

Solution 4

document.getElementById("Label6").innerHTML = name;
}



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

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100